Problem With Health Potion

So I am trying to make a shop system for my game but I have encountered some problems. The shop works fine, the problem is that a tool which is distributed from the shop. When the item is in StarterPack the script works fine but when I put the tool in ReplicateStorage it breaks. Here’s the script I use.

local potion = script.Parent
local player = potion.Parent

potion.Activated:Connect(function()
	local humanoid = player:FindFirstChild("Humanoid")
	if humanoid then
       for i = 1, 6, 1 do
         humanoid.Health = humanoid.Health + 5
         else print("no")
       end
	end
	potion:Destroy()
end)

The above script is a script btw not a local script and I am only asking for suggestions. Thanks a lot!

3 Likes

Can you show the script that moves this tool to the character?

You added an else that shouldn’t be there. There’s also no need for a loop if you aren’t going to add a wait. A more optimized version would be this:

local potion = script.Parent
local player = potion.Parent

potion.Activated:Connect(function()	
	local humanoid = player:FindFirstChildWhichIsA("Humanoid")
	
	if not humanoid then
		return
	end
	
	potion.Enabled = false
	
	humanoid.Health += 30
	
	potion:Destroy()
end)

Below this line add this and check the output (check every occurance it being printed):
print("Debug", player)

2 Likes
local Gamer = game:GetService("Players").LocalPlayer
local potion = game:GetService("ReplicatedStorage").Tools.HealthPotion

script.Parent.MouseButton1Click:Connect(function()
	if Gamer.leaderstats.Money.Value >= 200 then
		Gamer.leaderstats.Money.Value = Gamer.leaderstats.Money.Value - 200
		potion:Clone().Parent = Gamer.Backpack
	end
end)

By the way, the loop did have a wait but I think I had forgotten to add it back in after I tried to edit it a bit more.

Uhh, you should not be handling transactions on the client, because they won’t replicate to the server. This is why your script isn’t working, because you’re cloning it on the client, and not on the server.

Use RemoteEvents, because doing this locally will not work.

2 Likes

I will try to see what I could.

Here’s a template. See if this works:

local healAmount = 20

script.Parent.Activated:Connect(function()
    local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
    local character = player.Character
    local humanoid = character.Humanoid
    
    if humanoid.Health <= (humanoid.MaxHealth-healAmount) then
        humanoid.Health += healAmount
    else
        humanoid.Health = 100
    end
    print("Used potion")
    script.Parent:Destroy()
end)

Make sure the script is parented under the tool.

1 Like

Is it possible to make it a for i loop? I’m trying to make it heal gradually overtime.

1 Like

If you’re using loops, then this can then delay your function. You need to add a debounce.

local enabled = false
local healAmount = 20
local delayPerHeal = 0.1

script.Parent.Activated:Connect(function()
    
    if enabled == false then return end
    enabled = true
    
    local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
    local character = player.Character
    local humanoid = character.Humanoid
    
    for i = 1,healAmount do
        if humanoid.Health ~= humanoid.MaxHealth then
            humanoid.Health += 1
        end
        task.wait(delayPerHeal)
    end
    
    print("Used potion")
    script.Parent:Destroy()
    
end)

Try this and see if it works, perhaps.

1 Like

It isn’t healing the player, is it maybe because it has to be a local script?

1 Like

Do all of this in a server script.
If it’s local, the health is local, so others can’t see it.

1 Like

Its all in a script but for some reason the loops won’t work.

1 Like

Put a print in the loop. If it works then it’s something with the actual health being set.

1 Like

The script activates but the for i loop doesn’t work nor does the script.Parent:Destroy().

1 Like

Try this, instead of a loop, we can smoothly set the health using TweenService.

local TS = game:GetService("TweenService")

local enabled = false
local healAmount = 20
local healDuration = 5

script.Parent.Activated:Connect(function()
    
    if enabled == false then return end
    enabled = true
    
    local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
    local character = player.Character
    local humanoid = character.Humanoid
    
    local tween = TS:Create(humanoid,TweenInfo.new(healthDuration,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{Health = (humanoid.Health+20)})
    
    if humanoid.Health <= (humanoid.MaxHealth-healAmount) then
        tween:Play()
    else
        humanoid.Health = humanoid.MaxHealth
    end
    
    tween.Completed:Wait()
    
    print("Used potion")
    script.Parent:Destroy()
    
end)
1 Like

I just edited it, copy and paste it again.

1 Like

You’re missing the problem. He’s cloning the tool on the client side, but the script is on the server side. The server side code will never run, because on the server side, the tool is still in ReplicatedStorage.

1 Like

You’re spawning the tool on the client side?

1 Like

It still won’t work. Here let me show you a video.

Try to make a local script > remote event > server script then?