How To Make A Script To Buy SOmething?

I’m making a mall in my game, and the mall is the place where players can spend their money. I want to make a script that gives a player their pizza if they have 15 or more Cash, but the parts where the Cash value in leaderstats is changed don’t work.

Script:

function Clicked(player)

if game.Players.LocalPlayer.leaderstats.Cash.Value >= 15 then
	
	local y = player.Backpack
local z = game.Lighting["Pizza"]
z:Clone().Parent = y  
game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value - 15
script.Parent.Purchase:Play()
	
end

end

script.Parent.ClickDetector.MouseClick:connect(Clicked)

Is this in a local or server script? I see so many security flaws here that I need more info

Oh, a server script.
(30 Characters)

found your problem then - you can’t use localplayer in a server script.

But if I place it in a local script, will the other players be able to see the pizza and see the Cash value change?

nope. You will have to use Remotes to pull this system off.

Actually - you can use the Player argument of the Click detector and just keep it in a server script

No. Think LocalScript as front-end and the Server Script as the back-end.
I’d use RemoteEvents/RemoteFunctions for this.
May I ask, why are you using LocalPlayer for a server script?

I’m kind of new at scripting, so I didn’t know that you couldn’t use LocalPlayer in a ServerScript.

Take a look at this article which explains the client-server model of Roblox. This should help you avoid such mistakes as using localplayer on the server

I’ve posted a solution in my previous reply so give that a look

1 Like