So, I’m trying to parent something to the player via a local script. Since that can break some server-side scripts, I though sending an event to the server would work
This is the code i have for the local script:
local players = game:GetService("Players")
local player = players.LocalPlayer
local backpack = player.Backpack
local craftFolder = game.ReplicatedStorage.Crafting
local craftedItem = craftFolder:WaitForChild("57")
local CraftPart1 = "Block"
local CraftPart2 = "Sphere"
local CP1 = backpack:WaitForChild(CraftPart1)
local CP2 = backpack:WaitForChild(CraftPart2)
local Crafted = game.ReplicatedStorage:WaitForChild("CraftedItem")
script.Parent.MouseButton1Click:Connect(function()
if backpack:FindFirstChild(CraftPart1) and backpack:FindFirstChild(CraftPart2) then
CP1:Destroy()
CP2:Destroy()
Crafted:FireServer(player, craftedItem)
end
end)
And the Server script:
local Crafted = game.ReplicatedStorage:WaitForChild("CraftedItem")
Crafted.OnServerEvent:Connect(function(player, craftedItem)
print(craftedItem)
print(player)
local item = craftedItem:Clone()
item.Parent = player.Backpack
end)
However, on activating the event i get this error and these values printed:
15:18:03.051 AL_phaOmega - Server - ItemSender:4
15:18:03.052 AL_phaOmega - Server - ItemSender:5
15:18:03.052 ServerScriptService.ItemSender:7: attempt to index nil with 'Parent' - Server - ItemSender:7
15:18:03.052 Stack Begin - Studio
15:18:03.053 Script 'ServerScriptService.ItemSender', Line 7 - Studio - ItemSender:7
15:18:03.053 Stack End - Studio
It’s printing my username as the CraftedItem, when it should be “57”
Any help??
1 Like
There is no need to pass the player as argument in the LocalScript, that’s done automatically.
It shouldn’t be
Crafted:FireServer(player, craftedItem)
But instead should be
Crafted:FireServer(craftedItem)
Because roblox automatically assigns a player variable on the server side already.
I still get the issue of the CraftedItem value being the Player’s name, And how would i add the item to the player’s backpack without the value?
Show the code with the changes and make sure the .OnServerEvent
event has the player and craftedItem arguments.
1 Like
Make sure u did the changes on the localscript at line 22
local players = game:GetService("Players")
local player = players.LocalPlayer
local backpack = player.Backpack
local craftFolder = game.ReplicatedStorage.Crafting
local craftedItem = craftFolder:WaitForChild("57")
local CraftPart1 = "Block"
local CraftPart2 = "Sphere"
local CP1 = backpack:WaitForChild(CraftPart1)
local CP2 = backpack:WaitForChild(CraftPart2)
local Crafted = game.ReplicatedStorage:WaitForChild("CraftedItem")
script.Parent.MouseButton1Click:Connect(function()
if backpack:FindFirstChild(CraftPart1) and backpack:FindFirstChild(CraftPart2) then
CP1:Destroy()
CP2:Destroy()
Crafted:FireServer(craftedItem)
end
end)
local Crafted = game.ReplicatedStorage:WaitForChild("CraftedItem")
Crafted.OnServerEvent:Connect(function(craftedItem)
print(craftedItem)
local item = craftedItem:Clone()
item.Parent = player.Backpack -- I dunno how to reference the player now lol
end)
15:30:52.568 AL_phaOmega - Server - ItemSender:4
15:30:52.568 ServerScriptService.ItemSender:6: attempt to index nil with 'Backpack' - Server - ItemSender:6
15:30:52.569 Stack Begin - Studio
15:30:52.569 Script 'ServerScriptService.ItemSender', Line 6 - Studio - ItemSender:6
15:30:52.569 Stack End - Studio
You still need the player on the server.
1 Like
On the server keep the player argument.
1 Like
The server script should look like this:
local Crafted = game.ReplicatedStorage:WaitForChild("CraftedItem")
Crafted.OnServerEvent:Connect(function(player, craftedItem)
print(craftedItem)
local item = craftedItem:Clone()
item.Parent = player.Backpack -- I dunno how to reference the player now lol
end)
That fixed it, Gonna play around a bit to make sure that the error doesnt resurface
Glad we could help you with your issue, you should mark their answer as a solution though so everyone knows that your issue has been resolved.
1 Like
It shouldn’t happen again, always make sure that when you fire a RemoteEvent on the client, you don’t pass the player argument but when you listen to the RemoteEvent on the server, you always have to reference the player as the first argument.