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

How do I get a function in a local script to return two values?

Asked by 4 years ago

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.

2 answers

Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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

0
Thanks, this works perfectly. lemark23 7 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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)

Answer this question