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!
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)
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.
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)
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, 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)
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.