I am getting this error while making a gun system.
script.Parent.DamageEvent.OnServerEvent:Connect(function(damage,object) local human = object.Parent:FindFirstChild("Humanoid") local gib = script.Gib:Clone() gib.FX.Enabled = true gib.CFrame = CFrame.new(object.CFrame.X,object.CFrame.Y,object.CFrame.Z) game.Debris:AddItem(gib,0.35) if human~=nil then human:TakeDamage(damage) end end)
Does anyone know how does that happen and how do you fix it??
So, the :FireServer()
function has thing where the 1st argument is automaticlly set to the localplayer. So pretty much if you're doing.
--Local remote:FireServer(1235, true) --Server remote.OnServerEvent:Connect(function(number, bool) print(number) print(bool) end)
Now this should technaclly print 1235 and true, because those are the values that we fired from the local script, but no actually, it will print the player's name, and then 1235. And that's because the 1st argument is always the player, even if you didn't set it to the player. The :FireServer() will set it as the 1st argument, and let's say if you got another argument that you see as the first one, it will be second, and all arguments will be moved by one. That's what happened wiht the script that I wrote. the number paramater was the player cuz that's always first, but number which was the 1st paramater became second. And that's exactly the problem with your script. You set dmg which is a number value as a first argument, and i'm assuming that the object is the player. since as we said player is always the first argument by default. dmg became the player instance, and the number value stored as dmg became the object argument.
So this should be your script.
script.Parent.DamageEvent.OnServerEvent:Connect(function(player,dmg) local human = player.Parent:FindFirstChild("Humanoid") local gib = script.Gib:Clone() gib.FX.Enabled = true gib.CFrame = CFrame.new(player.CFrame.X,player.CFrame.Y,player.CFrame.Z) game.Debris:AddItem(gib,0.35) if human~=nil then human:TakeDamage(dmg) end end)
And don't forget to change the arguments in the local script. The player is set to first, and the damage is second.