I am trying to make an RPG. But I'm really confused as this script isn't working. I mean, it works in Studio, but not online. I have also tried converting this script to a local script .
This script is supposed to buy a weapon in my game.
Here is the script.
script.Parent.MouseButton1Click:connect(function() local RS = game:GetService("ReplicatedStorage") local item = RS:WaitForChild("Old Sword") local price = 100 local player = game.Players.LocalPlayer local stats = player:WaitForChild("leaderstats") if stats.Gold.Value == price then stats.Gold.Value = stats.Gold.Value - price local cloned = item:Clone() local cloned2 = item:Clone() cloned2.Parent = player.Backpack cloned.Parent = player.StarterGear end end)
(server script)
I'd really appreciate it if anyone teaches me what I did wrong with this script.
Assuming this is a TextButton
, here is a fixed version of you script:
Make sure this is a LocalScript Inside the Button.
local Button= script.Parent local RS = game:GetService("ReplicatedStorage") local item = RS:WaitForChild("Old Sword") local price = 100 local player = game.Players.LocalPlayer local stats = player:WaitForChild("leaderstats") Button.MouseButton1Down:Connect(function() -- avoid :connect because it's deprecated if stats.Gold.Value >= price then -- you don't want it exactly equal to price so if equal or bigger stats.Gold.Value = stats.Gold.Value - price local cloned = item:Clone() local cloned2 = item:Clone() cloned2.Parent = player.Backpack cloned.Parent = player.StarterGear end end)
And that should fix your script. In the game, you probably have a bit more money, you if
statement states if you have EXACTLY the same amount which should be rare.
Hopefully this helped, remember LocalScript
inside the button
Best of luck developer!
Since LocalPlayer
is nil on the server, and you’re using a MouseButton1Click
event, both are client side only. Convert it to a LocalScript
.