Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[SOLVED] Why does OnServerEvent returns more arguments than expected?

I have a local script which is firing a remote event but whenever i receive the remote event on the server side the server outputs all the clients as players who touched the object.

The code looks something like this:

local script

local sell = game.Workspace:WaitForChild("Sell").OpenShop.Sellpart
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local test = game.StarterGui.ScreenGui.TextButton

sell.Touched:Connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local player = game.Players[hit.Parent.name]
		ReplicatedStorage.Remotes.Sell:FireServer(player)
	end
end)

Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
local sell = game.Workspace:WaitForChild("Sell").OpenShop.Sellpart

ReplicatedStorage.Remotes.Sell.OnServerEvent:Connect(function(player)
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + player.invisstats.Strength.Value
	player.invisstats.Strength.Value = 0
	player.PlayerGui:WaitForChild("Coins").Coinnumber.Text = (player.leaderstats.Coins.Value)
	player.PlayerGui:WaitForChild("Coins").CoinShadow.Text = player.PlayerGui:WaitForChild("Coins").Coinnumber.Text
end)

2 answers

From the client, when you fire the server event, you're passing in the player augment, as you can see here:

ReplicatedStorage.Remotes.Sell:FireServer(player)

But you don't need to pass in that argument. Removing the player argument, you'll still be able to get the player from the server because player is the first argument by default.

Fixed code:

Local script

local sell = game.Workspace:WaitForChild("Sell").OpenShop.Sellpart
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local test = game.StarterGui.ScreenGui.TextButton

sell.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local player = game.Players[hit.Parent.name]
		ReplicatedStorage.Remotes.Sell:FireServer()
	end
end)

Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
local sell = game.Workspace:WaitForChild("Sell").OpenShop.Sellpart

ReplicatedStorage.Remotes.Sell.OnServerEvent:Connect(function(player)
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + player.invisstats.Strength.Value
	player.invisstats.Strength.Value = 0
	player.PlayerGui:WaitForChild("Coins").Coinnumber.Text = (player.leaderstats.Coins.Value)
	player.PlayerGui:WaitForChild("Coins").CoinShadow.Text
	player.PlayerGui:WaitForChild("Coins").Coinnumber.Text
end)

Read more about it here

1 comment

0

Didn't help my problem. When one player touches the part the server still thinks that all the players in the server has touched the part. For example if i added print between line 5 and 6 on the Script and printed player, the output would be everyone that's currently on the server. I only want the one who initially touched the part to be outputted.

Local scripts only run when they are a descendant of something that is individual to the player. The part known as "Sell" was not individual to the player. Therefore to fix it i had to include this line of code below line 5. The code should have looked like this:

Local script

local sell = game.Workspace:WaitForChild("Sell").OpenShop.Sellpart
local ReplicatedStorage = game:GetService("ReplicatedStorage")

sell.Touched:Connect(function(hit)
	if hit.Parent == game.Players.LocalPlayer.Character then
		local player = game.Players[hit.Parent.name]
		ReplicatedStorage.Remotes.Sell:FireServer(player)
	end
end)

Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
local sell = game.Workspace:WaitForChild("Sell").OpenShop.Sellpart
 
ReplicatedStorage.Remotes.Sell.OnServerEvent:Connect(function(player)
	print(player)
    player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + player.invisstats.Strength.Value
    player.invisstats.Strength.Value = 0
    player.PlayerGui:WaitForChild("Coins").Coinnumber.Text = (player.leaderstats.Coins.Value)
    player.PlayerGui:WaitForChild("Coins").CoinShadow.Text = (player.leaderstats.Coins.Value)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.