Hello, i have been learning a lot about Filtering Enabled lately but i still do not get it. I started writing this script but it only works in Roblox Studio. Can someone please help me?
Inside a textbutton in a local script
RS = game.ReplicatedStorage.CollectCashEvent script.Parent.MouseButton1Click:Connect(function() RS:FireServer(50) end)
Inside the textbutton in a normal script
user = game.Players.LocalPlayer RS = game.ReplicatedStorage.CollectCashEvent RS.OnServerEvent:Connect(function() user.leaderstats.Cash.Value = user.leaderstats.Cash.Value + number end)
game.Players.LocalPlayer --cant be used in a server script aka normal only local scripts
You cant use game.Players.LocalPlayer in a script but u can use it in a local script
So.. change the script in the textlabel to a regular script make the one that runs it a local and instead of OnServerEvent or FireServer so
--Regular Script Event:FireServer() --and then --local script Event.OnClientEvent:connect(function() --Code when ran end)
You should probably let the server determine the amount of Cash that the player is given.
Server script:
RS = game.ReplicatedStorage.CollectCashEvent number = 50 -- Make sure Player is in the function arguments so we know what player to give it to RS.OnServerEvent:Connect(function(Player) Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + number end)
Local script:
RS = game.ReplicatedStorage.CollectCashEvent script.Parent.MouseButton1Click:Connect(function() RS:FireServer() end)
The problem with your event is that instead of using the Client argument that is returned automatically with the server event, you're trying to use Players.LocalPlayer
, which can't be accessed from a server script.
Instead what you should be doing is:
RS = game.ReplicatedStorage.CollectCashEvent RS.OnServerEvent:Connect(function(client) client.leaderstats.Cash.Value = client.leaderstats.Cash.Value + number end)
The RemoteEvent automatically returns the Player that fired it as the first argument.