So I have a LocalScript in the player's on screen GUI and I can't get it to return the two values I want. It only returns the one that is specified first.
The LocalScript:
B1.MouseButton1Down:Connect(function() if visible == true and player.StatsSpent.Value < player.leaderstats.Level.Value then game.ReplicatedStorage.StatSpend:FireServer(player, 1) end end)
This function only returns the first value (in this example, the player). When I switch it to look like this: game.ReplicatedStorage.StatSpend:FireServer**(1, player)**
, it will only return the number. I confirmed this by printing in the server script. I want it to pass the two values to the server script so I can confirm the player's identity and give them a specified number of points.
The Server Script:
game.ReplicatedStorage.StatSpend.OnServerEvent:Connect(function(amount,plr) print("StatSpend Call Received 1") print("plr =", plr) print("amount =", amount) if player.Name == plr.Name then StatsSpent.Value = StatsSpent.Value + amount print("StatSpend Call Received 2") end end)
When I return the number first in the LocalScript, then it cannot confirm the player's identity here: if player.Name == plr.Name then
because plr
is the number just like amount
. But if I return the player first, then the score can't be increased here: StatsSpent.Value = StatsSpent.Value + amount
because amount
is the player just like plr
.
If anyone knows where I have something wrong or if they need more information, please leave a reply.
Hello, lemark23!
I've found some errors on your code:
First of all, you're using the arguments on different orders: game.ReplicatedStorage.StatSpend:FireServer(player, 1)
Here you send the player, and after that a number, on the server, game.ReplicatedStorage.StatSpend.OnServerEvent:Connect(function(amount,plr)
you define the first argument as being the number, and the second one as being the player.
Also, your script is sending the player 2 times to the server, as the :FireServer()
event aways send the player, as roblox makes that for script security.
Your scripts should be:
--Local Script B1.MouseButton1Down:Connect(function() if visible == true and player.StatsSpent.Value < player.leaderstats.Level.Value then game.ReplicatedStorage.StatSpend:FireServer(1) end end)
--Server Script game.ReplicatedStorage.StatSpend.OnServerEvent:Connect(function(plr, amount) print("StatSpend Call Received 1") print("plr =", plr) print("amount =", amount) if player.Name == plr.Name then StatsSpent.Value = StatsSpent.Value + amount print("StatSpend Call Received 2") end end)
Also, answering to your question, Yes, you can send more than one argument to a server event
When you fire an event from client to server, the first parameter in the OnServerEvent function by default is the player object. So when you create the OnServerEvent function, your parameters should be (player, arg1, arg2, ...):
game.ReplicatedStorage.StatSpend.OnServerEvent:Connect(function(player, arg1, arg2, ...)
You don't have to pass player when you call FireServer. So in the local script I would call:
game.ReplicatedStorage.StatSpend:FireServer(1)
then for the OnServerEvent function have parameters (player, amount)
game.ReplicatedStorage.StatSpend.OnServerEvent:Connect(function(player, amount)