Advertisement
Sungmingamerpro13

MedkitScript (Quantity System)

May 6th, 2024 (edited)
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 5.64 KB | None | 0 0
  1. --- LocalScript ---
  2.  
  3. local PS = game:GetService("Players")
  4.  
  5. local player = PS.LocalPlayer
  6. local mouse = player:GetMouse()
  7. local tool = script.Parent
  8. local heal = tool:WaitForChild("Heal")
  9. local highlights = {} -- Table to store the selection box instances
  10. local beam = nil -- Initialize the beam variable
  11. local currentTarget = nil -- Track the current player being hovered over
  12. local QuantityLabel = player.PlayerGui:WaitForChild("BackpackGui").Frame.quantityLabel
  13.  
  14. tool.Equipped:Connect(function()
  15.     -- Create selection boxes for all players
  16.     for _, v in ipairs(PS:GetPlayers()) do
  17.         if v ~= player then
  18.             local char = v.Character
  19.             if char and char:IsA("Model") then
  20.                 local charPart = char
  21.                 if charPart then
  22.                     local highlight = Instance.new("SelectionBox")
  23.                     highlight.Color3 = Color3.fromRGB(0, 249, 0)
  24.                     highlight.LineThickness = 0.1
  25.                     highlight.Adornee = charPart
  26.                     highlight.Parent = charPart
  27.                     highlights[v] = highlight
  28.                 end
  29.             end
  30.         end
  31.     end
  32.  
  33.     if not beam then
  34.         beam = Instance.new("Beam")
  35.         beam.Color = ColorSequence.new(Color3.fromRGB(0, 249, 0)) -- Set the beam color to green
  36.         beam.Parent = workspace
  37.     end
  38.  
  39.     mouse.Move:Connect(function()
  40.         local targetPlayer = nil
  41.  
  42.         -- Check if the mouse is hovering over any player's character
  43.         for _, v in ipairs(PS:GetPlayers()) do
  44.             if v ~= player then
  45.                 local char = v.Character
  46.                 if char and char:IsA("Model") then
  47.                     local charPart = char:FindFirstChild("HumanoidRootPart")
  48.                     if charPart then
  49.                         local mouseHit = mouse.Hit.p
  50.                         local charPos = charPart.Position
  51.                         local distance = (mouseHit - charPos).Magnitude
  52.  
  53.                         -- If the mouse is within a certain distance of the player's character, consider it a hover
  54.                         if distance <= 5 then
  55.                             targetPlayer = v
  56.                             break
  57.                         end
  58.                     end
  59.                 end
  60.             end
  61.         end
  62.  
  63.         if targetPlayer ~= currentTarget then
  64.             -- Remove the beam if a new target is being hovered
  65.             if beam then
  66.                 if beam.Attachment0 then
  67.                     beam.Attachment0:Destroy()
  68.                 end
  69.                 if beam.Attachment1 then
  70.                     beam.Attachment1:Destroy()
  71.                 end
  72.             end
  73.  
  74.             if targetPlayer then
  75.                 -- Create and attach an attachment to the player's and the closest player's root parts
  76.                 local attachment0 = Instance.new("Attachment")
  77.                 attachment0.Parent = player.Character.HumanoidRootPart
  78.                 local attachment1 = Instance.new("Attachment")
  79.                 attachment1.Parent = targetPlayer.Character.HumanoidRootPart
  80.  
  81.                 -- Set the Attachments as the end points of the beam
  82.                 if beam then
  83.                     beam.Attachment0 = attachment0
  84.                     beam.Attachment1 = attachment1
  85.                 end
  86.             end
  87.  
  88.             currentTarget = targetPlayer
  89.         end
  90.     end)
  91. end)
  92.  
  93. tool.Unequipped:Connect(function()
  94.     -- Remove selection boxes when unequipping the tool
  95.     for _, highlight in pairs(highlights) do
  96.         highlight:Destroy()
  97.     end
  98.     highlights = {}
  99.  
  100.     -- Remove the beam attachments and destroy the beam when unequipping the tool
  101.     if beam then
  102.         if beam.Attachment0 then
  103.             beam.Attachment0:Destroy()
  104.         end
  105.         if beam.Attachment1 then
  106.             beam.Attachment1:Destroy()
  107.         end
  108.  
  109.         beam:Destroy()
  110.         beam = nil
  111.     end
  112.  
  113.     currentTarget = nil
  114. end)
  115.  
  116. tool.AncestryChanged:Connect(function(_, parent)
  117.     -- Remove selection boxes and the beam when the tool is destroyed
  118.     if not parent then
  119.         for _, highlight in pairs(highlights) do
  120.             highlight:Destroy()
  121.         end
  122.         highlights = {}
  123.  
  124.         if beam then
  125.             if beam.Attachment0 then
  126.                 beam.Attachment0:Destroy()
  127.             end
  128.             if beam.Attachment1 then
  129.                 beam.Attachment1:Destroy()
  130.             end
  131.  
  132.             beam:Destroy()
  133.             beam = nil
  134.         end
  135.  
  136.         currentTarget = nil
  137.     end
  138. end)
  139.  
  140. tool.Activated:Connect(function()
  141.     if currentTarget then
  142.         heal:FireServer(currentTarget)
  143.     else
  144.         -- If no other player is targeted by the mouse hover, heal the player using the tool
  145.         heal:FireServer(player)
  146.  
  147.         if tool:WaitForChild("stacks").Value == 0 then
  148.             QuantityLabel.Visible = false
  149.         end
  150.     end
  151. end)
  152.  
  153. tool:WaitForChild("stacks").Changed:Connect(function(Value)
  154.     QuantityLabel.Text = "Quantity: "..Value
  155. end)
  156. QuantityLabel.Text = "Quantity: "..tool.stacks.Value
  157.  
  158. tool.Equipped:Connect(function()
  159.     QuantityLabel.Visible = true
  160. end)
  161.  
  162. tool.Unequipped:Connect(function()
  163.     QuantityLabel.Visible = false
  164. end)
  165.  
  166. --- Script ---
  167.  
  168. local tool = script.Parent
  169. local heal = tool:WaitForChild("Heal")
  170. local remainingUses = 3  -- Variable to track the remaining uses
  171.  
  172. local RunService = game:GetService("RunService")
  173.  
  174. local function playHealEffect(character)
  175.     local core = game.ReplicatedStorage:FindFirstChild("healingcore")  -- Assuming the part is in ReplicatedStorage
  176.  
  177.     if core then
  178.         local rootPart = character:FindFirstChild("HumanoidRootPart")
  179.         if rootPart then
  180.             core = core:Clone()
  181.             core.Parent = rootPart
  182.             core.CFrame = rootPart.CFrame  -- Set the initial CFrame to match the HumanoidRootPart
  183.  
  184.             local weld = Instance.new("WeldConstraint")
  185.             weld.Parent = core
  186.             weld.Part0 = core
  187.             weld.Part1 = rootPart
  188.  
  189.             wait(2)  -- Wait for 2 seconds
  190.             weld:Destroy()  -- Remove the WeldConstraint
  191.             core:Destroy()  -- Destroy the part after 2 seconds
  192.         end
  193.     end
  194. end
  195.  
  196.  
  197. heal.OnServerEvent:Connect(function(player, target)
  198.     local char = target.Character
  199.     local hum = char:WaitForChild("Humanoid")
  200.  
  201.     if hum and hum.Health < hum.MaxHealth and remainingUses > 0 then
  202.         hum.Health += 50
  203.         remainingUses -= 1
  204.  
  205.         script.Parent.Sound.Playing = true
  206.         playHealEffect(char) -- Call the function to play the heal effect
  207.  
  208.         if remainingUses <= 0 then
  209.             tool.stacks.Value -= 1
  210.             if tool.stacks.Value == 0 then
  211.                 player.Character.Humanoid:UnequipTools()
  212.                 wait(0.0000000000000000001)
  213.                 tool:Destroy()
  214.             end
  215.         end
  216.     end
  217. end)
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement