Passing variable through RemoteEvent gives a different object on the Server?

I’m trying to make a function which passes a player’s character if they have been hit by the client’s knife. The remote event fires with two parameters, damage (irrelevant here) and hit. Hit should be a limb of the player’s character which was hit. Problem is, on the server it thinks hit is my player object (in game.Players), not the person who I hit with the knife’s limb.

Client code:

function newBullet()
    if WeaponData.WeaponType == "Melee" then
			
		if shooting == false then
			
			shooting = true
			
			local muzzle = WeaponInHand:WaitForChild('Muzzle')
			local damage = 100

			recoilcf = WeaponData.ShootCFrame
			muzzle:WaitForChild("Fire"):Play()
			
			local blade = WeaponInHand.Handle
			
			blade.Touched:Connect(function(hit)
				if shooting == true then
					if hit.Parent:WaitForChild("Humanoid") then
						if hit.Parent ~= plr.Character then
							print(hit)
							Events.Stab:FireServer(damage,hit)
							print('fired')
						elseif hit.Parent == plr.Character then
							print('lol u hit urself idot')
						end
					end
				end
			end)
		
			wait(0.2)
			shooting = false
			
			wait(60/WeaponData.ShootRate)
			
		end
	end
end

Server code:

local RepStor = game.ReplicatedStorage
local Events = RepStor:WaitForChild("Events")

local function DamageStab(hit,damage)
	print(damage)
	print(hit) -- this gives my player's name, not the person who i hit!!
	if hit.Parent:FindFirstChild("Humanoid") then
		local humanoid = hit.Parent.Humanoid
		print('humanoid found and variable')
		if humanoid.Health > 0 then -- if enemey is alive
			humanoid:TakeDamage(damage)
			print('taking damage')
		end
	elseif hit.Parent.Humanoid == nil then
		print('humanoid nil')
	end
end

Events:WaitForChild("Stab").OnServerEvent:Connect(DamageStab)

When using a remote event on the client, it will give the player as well. You can fix this by doing

local function DamageStab(player,hit,damage)

end

Is this line of code to be on the server script or the client script?

It is supposed to be on the server.

Just replace local function DamageStab(hit,damage) with
local function DamageStab(player,hit,damage)

Alright, I’ll give it a try. :smile:

It worked, many thanks! I didnt realise the player parameter was passed through when using remote events no the client. :smiley:

1 Like