Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why don't I get anything when I click my GUI buy button?

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.

2 answers

✓ Accepted answer

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!

2 comments

0

Also, can you explain why this is?

0

this is what?

Since LocalPlayer is nil on the server, and you’re using a MouseButton1Click event, both are client side only. Convert it to a LocalScript.

This discussion is preserved as it was. New answers, comments, and votes are closed.