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)
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.
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.
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.
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)
-- 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)