Problem With Health Potion

No, I just made an event which clones from a script.

Look at his previous code he sent, it has LocalPlayer in the script, meaning he’s cloning it locally.

Make a remote event to let the server clone the tool, not the client.

local button = script.Parent
local event = game:GetService("ReplicatedStorage").Events.Tools.Shop.HealthPotionShopEvent

button.Activated:Connect(function()
	local player = game.Players.LocalPlayer
	event:FireServer(player)
	print("Event Fired!")
end)
local event = game:GetService("ReplicatedStorage").Events.Tools.Shop.HealthPotionShopEvent
local potion = game:GetService("ReplicatedStorage").Tools.HealthPotion

event.OnServerEvent:Connect(function(player)
	print("Event Recieved")
	if player.leaderstats.Money.Value >= 200 then
		player.leaderstats.Money.Value = player.leaderstats.Money.Value - 200
		potion:Clone().Parent = player.Backpack
		print("Item Added")
	end
end)

That would work. Try those scripts.

Just a reminder, here’s a shortcut to one of the lines:

Before:

player.leaderstats.Money.Value = player.leaderstats.Money.Value - 200

After:

player.Leaderstats.Money.Value -= 200

It’s the exact same thing.

I’m going to try the edited one because I just now saw it.

The scripts still don’t work. The scripts activate but I think the humanoid is the problem.

The humanoid isn’t the problem.

Do you know what the problem is then?

Try extracting the Tool to the actual StarterPack, ignore the tool spawning in for now.

See if the actual tool script works.

The for i loop won’t work after I add it in StarterPack, but the first script you sent me works. Since the for i loops won’t work I guess I can use a regular +25 health script.

Try the TweenService method I recommended, it can smoothly set the health to any number.

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)

So I tried debugging the script and the only thing that prints is “Tool Activated”. I literally cannot understand what the problem here is.

local TS = game:GetService("TweenService")

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

script.Parent.Activated:Connect(function()
	print("Tool Activated")

	if enabled == false then return end
	enabled = true
	print("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(healDuration,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{Health = (humanoid.Health+20)})

	if humanoid.Health <= (humanoid.MaxHealth-healAmount) then
		tween:Play()
		print("Tween Played")
	else
		humanoid.Health = humanoid.MaxHealth
		print("Health Set")
	end

	tween.Completed:Wait()

	print("Used potion")
	script.Parent:Destroy()

end)

You always return if enabled is false, but enabled starts as false, so this script will never run.

Switch that enabled == false to enabled == true.

Code:

local TS = game:GetService("TweenService")

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

script.Parent.Activated:Connect(function()
	print("Tool Activated")

	if enabled then return end
	enabled = true
	print("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(healDuration,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{Health = (humanoid.Health+20)})

	if humanoid.Health <= (humanoid.MaxHealth-healAmount) then
		tween:Play()
		print("Tween Played")
	else
		humanoid.Health = humanoid.MaxHealth
		print("Health Set")
	end

	tween.Completed:Wait()

	print("Used potion")
	script.Parent:Destroy()

end)

Thank you, that made it worked

I made an optimized version for you here, only use it if it works, if it doesn’t work, use your current one:

--//Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

--//Variables
local Tool = script.Parent

--//Controls
local healAmount = 20
local healDuration = 5

--//Functions
Tool.Activated:Connect(function()
	local player = Players:GetPlayerFromCharacter(script.Parent.Parent)
	local character = player and player.Character
	local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
	
	if not humanoid then
		return
	end
	
	Tool.Enabled = false

	local healthTween = TweenService:Create(humanoid, TweenInfo.new(healDuration, Enum.EasingStyle.Linear), {
		Health = math.min(humanoid.Health + 20, humanoid.MaxHealth)
	})
	
	healthTween:Play()
	healthTween.Completed:Wait()

	Tool:Destroy()
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.