Hi! I want to create a shop system. Sadly, when I press the buy button, nothing really happens. I put a print() inside the funcion that runs when clicked but even that doesn’t work. I’ve tried putting prints and debugging, as seen in the script. Please help!
--StarterGui.BuyTime.BuyButton.LocalScript
local RepStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer.PlayerGui
local leaderstats = LocalPlayer.leaderstats
local cornStat = leaderstats and leaderstats:FindFirstChild("corn")
local timeupgStat = leaderstats and leaderstats:FindFirstChild("timeupg")
local SurfaceGui = PlayerGui.SurfaceGui
local TimePrice = RepStorage:WaitForChild("TimePrice")
local TimePriceLabel = PlayerGui.BuyTime:WaitForChild("TimePriceLabel")
script.Parent.Activated:Connect(function()
print("works")
if cornStat.Value >= TimePrice then
cornStat.Value = cornStat.Value - TimePrice
TimePrice.Value = TimePrice.Value + TimePrice.Value * 0.1
timeupgStat.Value = timeupgStat.Value + 1
TimePriceLabel.Text = tostring(TimePrice.Value) .. "Corn"
else
print("broke")
end
end)
Something important I notice here is
Youre changing the player stats on client, meaning an exploiter can easily ruin and change it. Use remotes to handle it on server with sanity checks.
You could also try using :WaitForChild() to wait for the button to load.
Example of what I’m talking about:
local Button = script.Parent:WaitForChild("Button")-- wherever the button is located
script.Parent.MouseButton1Click:Connect(function()
print("Button pressed")
-- put action below the print
end)
Since it’s a button you need to use MouseButton1Click instead of Activated.
You also need to make it a normal script instead of a LocalScript (if you plan on having it in the Workspace). LocalScripts can only be run from the client.
You can use a LocalScript if you need it for LocalPlayer but you will have to move the SurfaceGui object to StarterGui or the player’s PlayerGui, then set the Adornee property of the SurfaceGui to the part you want it on.
I put MouseButton1Click earlier, I said that the localscript is inside of the button, and I had already put the gui into StarterGui and then set the adornee from the start.