Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script to change players health when activated not working?

Asked by 3 years ago

I'm making a first aid kit and it almost all works properly, adds to the players health but as soon as it adds to the players health less than a second later the players health goes back down to where it originally was.

local Tool = script.Parent 
local Player = game:GetService("Players").LocalPlayer.Name
local healAmount = 30
local sound = script.Parent:FindFirstChild("FirstAid")
print(Player)

Tool.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        print("Button1Down")
        local Playeruser = game.Workspace:WaitForChild(Player)
        local humanoid = Playeruser:FindFirstChildWhichIsA("Humanoid")
            local currentHealth = humanoid.Health
            local newHealth = currentHealth + healAmount
            humanoid.Health = newHealth
            sound:Play()
        wait(3.344)
        Tool:Destroy()


    end)
end)

Could someone tell me why? Thanks!

0
Is your script local? You can only change health on the server. Bankrovers 226 — 3y
0
Yeah it is, but how would I get the player with the server script CheeseBallGuy11 12 — 3y

1 answer

Log in to vote
0
Answered by
pwx 1581 Moderation Voter
3 years ago

You should be doing this on the Server, not Client.

local Tool = script.Parent
local healAmount = 30
local Sound = Tool:WaitForChild('FirstAid')
local beenUsed = false

local Players = game:GetService('Players')

Tool.Activated:Connect(function()
    if beenUsed then return end -- can only use once
    beenUsed  = true
    local Player = Players:GetPlayerFromCharacter(Tool.Parent)
    if Player then
        if Player.Character and Player.Character:FindFirstChild('Humanoid') then
            local Humanoid = Player.Character.Humanoid
            Humanoid.Health = Humanoid.Health + healAmount
            Sound:Play()
            wait(Sound.TimeLength)
            Tool:Destroy()
        end -- core checks
    end -- player check
end) -- onEquipped
Ad

Answer this question