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

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

Asked by 4 years ago
Edited 4 years ago

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.

0
Is this local or server script? Leamir 3138 — 4y
0
I said it was a server. L0RD_Bloxx -5 — 4y

4 answers

Log in to vote
1
Answered by
iOwn_You 543 Moderation Voter
4 years ago
Edited 4 years ago

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

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. MrSuperKrut 167 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Try cloning it to both backpack and starterpack.

Log in to vote
0
Answered by 4 years ago

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?

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. L0RD_Bloxx -5 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

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 iOwn_You 543 — 4y
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. Optimalpokemon123 37 — 4y
0
Also the player is literally the client. Optimalpokemon123 37 — 4y

Answer this question