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)