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

[SOLVED] Why does my character not heal more than once?

Asked by 3 years ago
Edited 3 years ago

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

2 answers

Log in to vote
0
Answered by 3 years ago

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

0
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. grandiosav4 62 — 3y
0
Heal them from server scripts. if its touch change the first line to script.Parent.Touched instead of click detector.mouseclick BulletproofVast 1033 — 3y
0
You can directly heal the player when they touch instead of updating a value. touched param is the part that touched (probably the players legs/feet) so do if touch.parent (which is the players character) :findfirstchild'humanoid' then add the health to the humanoid BulletproofVast 1033 — 3y
0
I tried healing the player directly from the server script but that didn't work either grandiosav4 62 — 3y
Ad
Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

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

Answer this question