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

Shop button not giving me the tool when I attempt to buy?

I created a shop GUI which opens and closes and I created a buy button for my tool but the thing is, it is supposed to give me the tool and take away my current money value (if my money reaches a minimum point, you would be allowed to buy it), it does not work, it does not give me the tool nor take away my money.

local Players = game.GetService("Players")
local SS = game:GetService("ServerStorage")
local stats = game.Players.player:WaitForChild("leaderstats")
script.Parent.MouseButton1Click.Connect(function(buy)
	if stats.Cash.Value >= 150 then
		stats.Cash.Value = stats.Cash.Value - 150
		local Item = SS.Tools["Pistol"]:Clone()
	    Item.Parent = Players.player.Backpack
	end
end)

What am I supposed to do? What did I do wrong?

EDIT: this script is a server script.

2 comments

0

Is this local or server script?

0

I said it was a server.

4 answers

Youre trying to access server storage on what I assume to be a localscript, which is impossible.

To get the player you should use game.Players.LocalPlayer and not game.Players.player

You should use a remote function to handle the purchase on the server, you can never trust the client especially with stuff like this.

Remote Events/Functions

1 comment

0

Yeah he is right, you need to have 2 scripts, one is server script in server script service and other is local script in button.

In the variable called stats instead of this:

local stats = game.Players.player:WaitForChild(“leaderstats”)

Do this:

local stats  = game.Players.LocalPlayer:WaitForChild(“leaderstats”)

See the difference?

1 comment

0

I only use localplayer for local scripts but I attempted to have the shop buy serversided. I am now trying to fix my script.

So the proper way to do this (if you're using FilteringEnabled which you should be) is to use 2 different scripts 1 local script and 1 server script. You'll want to clone the tool and subtract the money on the server-side and handle the clicking of the button on the local script and connect it to the server script using a remote event.

https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

Worth mentioning that GUIs do NOT replicate onto the server and should be dealt with 100% client-side. Also, you should probably not store the money value on the client and should create a separate value on the server likely in ServerStorage.

3 comments

0

He is not storing it on the client, hes storing it inside of player which is fine. Changes exploiters would make would only appear to them and the server will remain unchanged, just like it would on ReplicatedStorage

0

You're not wrong but personally I would actually even prefer to store them in ReplicatedStorage than in the player just cause I read somewhere that you can have memory leaks when a player leaves storing them like that but to each their own. Only reason I said ServerStorage is because that's usually what I usually do, either that or a ModuleScript with a table.

0

Also the player is literally the client.

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