When someone slap me i die

I was making my slap battles game. But when someone gets slapped they die.
The error is: Players.D3nielDev.Backpack.Glove.Hitbox.HitHandler:78: attempt to index nil with ‘UserId’
or when the player is holding it in hand: Workspace.D3nielDev.
Glove.Hitbox.HitHandler:78: attempt to index nil with ‘UserId’.

The code is:

local Tool = script.Parent.Parent
local DebrisService = game:GetService("Debris")

local Anim = script:WaitForChild("Animation")
local AnimTrack 

local hitChars = {}
local debounce = false
local MS = game:GetService("MarketplaceService")
local x2 = 124242859
local x5 = 124243041
Tool.Activated:Connect(function()
	if debounce then
		return
	end

	debounce = true

	local humanoid = Tool.Parent:FindFirstChildOfClass("Humanoid")

		AnimTrack = humanoid:LoadAnimation(Anim)
	
	AnimTrack:Play()
	wait(2.5)
	debounce = false
end)

Tool.Hitbox.Touched:Connect(function(hit)
	if hitChars[hit.Parent] or not debounce then
		return
	end
	if hit.Parent:FindFirstChild("Humanoid") then
		local eChar = hit.Parent
		local plrChar = Tool.Parent
		local SpectatorGloveID = 10
		if game.Players:GetPlayerFromCharacter(eChar) then
			local ePlr = game.Players:GetPlayerFromCharacter(eChar)
			
			if ePlr.Equipped.Value == SpectatorGloveID then
				return
			end
		end
		local eHumanRootPart = eChar:FindFirstChild("HumanoidRootPart")
		local plrHumanRootPart = plrChar:FindFirstChild("HumanoidRootPart")

		if plrHumanRootPart and eHumanRootPart then
			script.Disabled = true
			eChar.Humanoid.Sit = true

			local force = Instance.new("BodyVelocity", eHumanRootPart)
			force.MaxForce = Vector3.new(2,2,2) * math.huge
			local direction = (eHumanRootPart.CFrame.Position - plrHumanRootPart.CFrame.Position).Unit
			force.Velocity = (direction + Vector3.new(0,1,0)).Unit * 20

			local rotation = Instance.new("BodyAngularVelocity", eHumanRootPart)
			rotation.AngularVelocity = Vector3.new(1,1,1) * math.pi * math.random(1,5)
			rotation.MaxTorque = Vector3.new(2,2,2) * math.huge
			rotation.P = 5000

			DebrisService:AddItem(force,0.35)
			DebrisService:AddItem(rotation,0.35)
			
			local Ragdoll = require(game:GetService("ReplicatedStorage"):WaitForChild("RagdollCharacterV1"))
			local RiggedChar
			local Ragdolled = false
			if not RiggedChar then
				RiggedChar = Ragdoll:SetupRagdoll(eChar,true)
			end
			if not Ragdolled then
				RiggedChar.Ragdoll(4)
				wait(script.Parent.Parent.SoundHandler.Cooldown.Value - 0.5)
				RiggedChar.Unragdoll()

			end
			eChar.Humanoid.Sit = false

			local player = game.Players:GetPlayerFromCharacter(Tool.Parent)
			if MS:UserOwnsGamePassAsync(player.UserId,x2) and MS:UserOwnsGamePassAsync(player.UserId,x5) then
				player.leaderstats.Slaps.Value = player.leaderstats.Slaps.Value+ 10
			elseif MS:UserOwnsGamePassAsync(player.UserId,x2) then
				player.leaderstats.Slaps.Value = player.leaderstats.Slaps.Value + 2
			elseif  MS:UserOwnsGamePassAsync(player.UserId,x5) then
				player.leaderstats.Slaps.Value = player.leaderstats.Slaps.Value + 5
			else
				player.leaderstats.Slaps.Value = player.leaderstats.Slaps.Value + 1
			end
		end
		hitChars[hit.Parent] = true
		wait(2.5)
		script.Disabled = false
		hitChars[hit.Parent] = nil
	end
end)

Perhaps by the time the local player line executes the player has stopped equipping the tool, causing it to be under the player’s backpack instead? If so then the following function may help:

local function getPlayerByTool(tool: Tool): Player?
	if tool.Parent:IsA("Backpack") then 
		return tool:FindFirstAncestorWhichIsA("Player")
	else
		local model = tool:FindFirstAncestorWhichIsA("Model")
		if not model then return nil end
		return game.Players:GetPlayerFromCharacter(model)
	end
end

--in your code:
local player = getPlayerByTool(Tool)
if not player then
	--weird edge case, handle it as you wish
	return 
end
--rest of your code

Another possible explanation is that by the time the line runs the player character is dead or its descendants are destroyed, which also includes the tool you’re referencing. If so then you may want to move the local player line on top of the touched event, so the player is fetched before the tool destruction.

1 Like

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