So, I am making a simple game about collecting apples to heal yourself, you need to collect enough apples in order to not die. You take x amount of damage per second.
And the problem is that when I collect an apple the first time everything goes as planned and I get healed, but the second time I collect an apple I do not heal. I have not seen anyone else get the same issue and was wondering how to fix this. I also do not get any errors in the output.
Part of a script that tells a value to get updated to 20
game.ReplicatedStorage.heal.Value = 20
LocalScript that handles the new value and updates your health
local humanoid = game.Players.LocalPlayer.Character.Humanoid local heal = game:GetService("ReplicatedStorage"):WaitForChild("heal") while wait() do humanoid.Health = humanoid.Health + game.ReplicatedStorage.heal.Value if heal.Value >= 1 then heal.Value = 0 end end
oh i probably should've mentioned that you collect the apples by touching them, not clicking and the script that tells the localscript to heal you is in the apple
UPDATE : Ok so i found that the local script that handles the new value doesnt see that the value has changed the second time i grab an apple. Does anyone know why this is happening?
Log:
14:11:56.573 applePickupScript thinks that heal.Value = 20 - Server 14:11:56.574 Script says that actall value is 20 - Server - Script:2 -- Script that i used for debugging checking which script is right. 14:11:56.591 LocalScript thinks that heal.Value = 0 - Client - LocalScript:6
Check out this post for the soultion to my problem https://scriptinghelpers.org/questions/123520/solved-why-does-my-localscript-not-see-that-ive-changed-a-value
maybe you could place a serverscript inside the apples
assuming you have a clickdetector inside it:
script.Parent.ClickDetector.MouseClick:Connect(function(plr) local Hum = plr.Character:FindFirstChild("Humanoid") Hum.Health = Hum.Health + 20 end)
i havent tested it, frankly i dont know if it works but you could try.
i hope i helped
You should try directly heal for the person touched, not updating the value since you need to loop through, and if the value updates the script is also looping and heal the player, what you are gonna do is heal him ONCE only for each apple
here is the script, put it inside apple so it's serversided
script.Parent.Touched:Connect(function(hit) -- If hit if hit.Parent:FindFirstChild("Humanoid") == true then -- Check if humanoid exists local Humanoid = hit.Parent.Humanoid -- Locate the Humanoid Humanoid.Health = Humanoid.Health + 20 -- Heal the player 20 health script.Parent:Destroy() -- Destroy the apple (Because it's eaten (Who want to eat your eaten apple????)) end end