Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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!

0
Also, can you explain why this is? Littlenatnat8508 4 — 5y
0
this is what? BlackOrange3343 2676 — 5y
Ad
Log in to vote
-2
Answered by 5 years ago

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

Answer this question