Remote Event received from Server has different results than Remote Event fired from Client

Hi there. I’m kind of new to remote events so I don’t know how to use them properly. When I fire the remote event from the client, the results it prints out are right, but the results the server print out are incorrect. I would appreciate it if someone helped me and thanks!

Client

-- Services
local RS = game:GetService("ReplicatedStorage")
local PlayerS = game:GetService("Players")

-- Coins
local Coins = PlayerS.LocalPlayer.Coin_Stats.Coins.Value

-- Remote Event
local ShopEvent = RS:WaitForChild("ShopEvent")

-- Player
local player = PlayerS.LocalPlayer
local bag = player.Backpack

-- GUI
local gui = script.Parent
local shop = gui.Shop
local desc = shop.ItemDescription
local toolSF = shop.ToolsScrollingFrame
local buyB = desc.BuyButton
local toolT = {}

-- Item Description content
local itemName = desc.ItemName
local itemDmg = desc.Damage
local itemPrice = desc.Price
local itemAbility = desc.Ability
local itemImg = desc.ItemImage

table.insert(toolT, toolSF:GetChildren())

for i, descendant in pairs(toolSF:GetDescendants()) do 
	if descendant:IsA("ImageButton") then
		descendant.MouseButton1Click:Connect(function()
			itemName.Text = descendant.ItemName.Value
			itemDmg.Text = "Damage: ".. descendant.Damage.Value
			itemPrice.Text = "Price: $".. descendant.Price.Value
			itemAbility.Text = "Ability: ".. descendant.Ability.Value
			itemImg.Image = descendant:FindFirstChildWhichIsA("Tool").TextureId
			buyB.MouseButton1Click:Connect(function()
				ShopEvent:FireServer(player, descendant)
				print(player)
				print(descendant)
			end)
		end)
	end
end

Server

-- Services
local RS = game:GetService("ReplicatedStorage")
local PlayerS = game:GetService("Players")
local SS = game:GetService("ServerStorage")

-- Remote Event
local ShopEvent = RS:WaitForChild("ShopEvent")
local ToolGiven = RS:WaitForChild("ToolGiven")

-- Tools
local tools = SS:WaitForChild("Tools")

local function onEventRecieved(player, descendant)
	print(player)
	print(descendant)
	
	if descendant:IsA("ImageButton") then
	local Coins = player.Coin_Stats.Coins.Value
	if Coins >= descendant.Price.Value then
		Coins -= descendant.Price.Value
		local toolGiven = tools:FindFirstChild(descendant)
		local toolClone = toolGiven:Clone()
		toolClone.Parent = player.Backpack
		ToolGiven:FireClient(player)
		end
	else 
		return end
	end
PlayerS.PlayerAdded:Connect(function(plr)
	ShopEvent.OnServerEvent:Connect(onEventRecieved)
end)

Output
image

The First Parameter in RemoteEvent.OnServerEvent is Ignored and is already given to you, so the Second Parameter (which in this case is called descendant) would be the first Argument when using :FireServer() on a RemoteEvent firing to the Server, you usually add the player argument for :FireClient(), the first argument is the Player, then the other arguments.

Okay, so do I just remove the player parameter on the client? Or do I do something else?

2 Likes

:+1:

ShopEvent:FireServer(descendant) -- would look like this

Do I remove anything on the server?

No.

It’s still printing the descendant as the player
image

Whenever you fire a remote from the client to server, the first argument will ALWAYS be the player object. You can not “get rid” of this in any way, but you don’t need to use the argument.

RemoteEvent.OnServerEvent:Connect(function(player, ...)

Above is an example. The first argument will ALWAYS be the player instance, no matter what you name it. Every other argument you passed initially will come afterward.

I’m not sure if this is related to the issue, but you should to move the OnServerEvent out of the PlayerAdded function, as it is not necessary.

--PlayerS.PlayerAdded:Connect(function(plr)

--end)
ShopEvent.OnServerEvent:Connect(onEventRecieved) -- move this out of the function

Could you please show your current code for both server and local script?

In short, you need to create arguments after it. If you are passing 3 arguments, you’ll need four arguments on the server (the first will always be the player instance)

Im basically saying to replace this part on your Client Script

ShopEvent:FireServer(player, descendant)

With this:

ShopEvent:FireServer(descendant)

So I’m not really sure what would be the issue then.

Client:

-- Services
local RS = game:GetService("ReplicatedStorage")
local PlayerS = game:GetService("Players")

-- Coins
local Coins = PlayerS.LocalPlayer.Coin_Stats.Coins.Value

-- Remote Event
local ShopEvent = RS:WaitForChild("ShopEvent")

-- Player
local player = PlayerS.LocalPlayer
local bag = player.Backpack

-- GUI
local gui = script.Parent
local shop = gui.Shop
local desc = shop.ItemDescription
local toolSF = shop.ToolsScrollingFrame
local buyB = desc.BuyButton
local toolT = {}

-- Item Description content
local itemName = desc.ItemName
local itemDmg = desc.Damage
local itemPrice = desc.Price
local itemAbility = desc.Ability
local itemImg = desc.ItemImage

table.insert(toolT, toolSF:GetChildren())

for i, descendant in pairs(toolSF:GetDescendants()) do 
	if descendant:IsA("ImageButton") then
		descendant.MouseButton1Click:Connect(function()
			itemName.Text = descendant.ItemName.Value
			itemDmg.Text = "Damage: ".. descendant.Damage.Value
			itemPrice.Text = "Price: $".. descendant.Price.Value
			itemAbility.Text = "Ability: ".. descendant.Ability.Value
			itemImg.Image = descendant:FindFirstChildWhichIsA("Tool").TextureId
			buyB.MouseButton1Click:Connect(function()
				ShopEvent:FireServer(player, descendant)
				print(player)
				print(descendant)
			end)
		end)
	end
end

Server:

-- Services
local RS = game:GetService("ReplicatedStorage")
local PlayerS = game:GetService("Players")
local SS = game:GetService("ServerStorage")

-- Remote Event
local ShopEvent = RS:WaitForChild("ShopEvent")
local ToolGiven = RS:WaitForChild("ToolGiven")

-- Tools
local tools = SS:WaitForChild("Tools")

PlayerS.PlayerAdded:Connect(function(player)

local function onEventRecieved(descendant)
	print(player)
	print(descendant)
	if descendant:IsA("ImageButton") then
	local Coins = player.Coin_Stats.Coins.Value
	if Coins >= descendant.Price.Value then
		Coins -= descendant.Price.Value
		local toolGiven = tools:FindFirstChild(descendant)
		local toolClone = toolGiven:Clone()
		toolClone.Parent = player.Backpack
		ToolGiven:FireClient(player)
		end
	else 
		return end
	end
	ShopEvent.OnServerEvent:Connect(onEventRecieved)
end)

I’ll try that but I think I already did and it didn’t work

I can’t believe that a small fix like that fixed the issue I was having! Thanks a lot!
image

1 Like

Thats literally what I said to do the first time

I thank you all for spending your time to help me!

I guess I mistyped it or something lol

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.