So I needed some help in a remote event. I put the server script in ‘ServerScriptService’
and remote event in a folder in ‘ReplicatedStorage’.
--server script
local re = game.ReplicatedStorage.Events.BuyCash
local cash = game.Players.LocalPlayer:WaitForChild("Cash")
re.OnServerEvent:Connect(function(num)
cash.Value = cash.Value + num
end
--local script in startergui
local re = game.ReplicatedStorage.Events.BuyCash
script.Parent.MouseButton1Click:Connect(function()
re:FireServer(100)
end)
Can someone please help me with this? It’s not giving any cash.
OnServerEvent receives the client who fired the remote as the first argument by default. Your num variable is currently referring to the player and the actual number that the client fires the remote with is discarded. Just add another parameter in OnServerEvent.
OnServerEvent:Connect(function (player, num)
Second thing is that server scripts can’t access LocalPlayer (it appears as nil), so use the player variable instead to get to their leaderstats to add the cash.
Please be sure to do basic debugging and research so you can resolve these problems easily or help us determine more information about your problem. This code would produce an error in the console so it’s good to provide a picture of what it says. OnServerEvent on the Developer Hub.
First and for most, you can’t get game.Players.LocalPlayer in Server, it can only be retrieved by the client.
Also, an easier way of doing cash.Value = cash.Value + num can be cash.Value += num.
And Num is read as the player, change it to, re.OnServerEvent:Connect(function(player, num).
There is a few problems with your code. The first problem I see is that you have a variable referring to the player on the server script. You can only use game.Players.LocalPlayer in a local script. The next problem is that you are giving an amount in cash on the client side which makes it very easy for hackers to exploit/change. Also, the OnServerEvent will take in the player as the first argument so that is where you should refer to the player.
Here is what the code should look like:
--server script
local re = game.ReplicatedStorage.Events.BuyCash
re.OnServerEvent:Connect(function(player)
player.Cash.Value += 100
end
--local script in startergui
local re = game.ReplicatedStorage.Events.BuyCash
script.Parent.MouseButton1Click:Connect(function()
re:FireServer()
end)