Not working equipped event

Basically I have small script that for some reason doesn’t work, exact problem is with equipped event, let me provide script and error (to be honest I have feeling this problem is easy to fix but I cant to find problem)

Script:

local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local RagdollModule = require(ReplicatedFirst.RagdollModule)


Players.PlayerAdded:Connect(function(plr)
	local Tool = plr.Backpack:FindFirstChild("Tool")
	plr.CharacterAdded:Connect(function(char)
		local hum = char:FindFirstChildOfClass("Humanoid")
		Tool.Equipped:Connect(function()
			hum.BreakJointsOnDeath = false
			hum.RequiresNeck = false
			Tool.Activated:Connect(function()
				RagdollModule.RagdollCharacter()
				task.wait(5)
				RagdollModule.UnRagdollCharacter()
			end)
		end)
	end)
end)

And error:
ServerScriptService.RagdollHandler:10: attempt to index nil with ‘Equipped’ - Server - RagdollHandler:10

Thanks.

– It looks like that part of the code runs when ‘Tool’ is not available in the Player’s backpack yet. Is the Tool is the StarterPack? In the meantime, try this code:

local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local RagdollModule = require(ReplicatedFirst.RagdollModule)


Players.PlayerAdded:Connect(function(plr)
	local Character = plr.Character or plr.CharacterAdded:Wait() -- Waits for Character to load
	local Tool = plr.Backpack:WaitForChild("Tool") -- Changed FindFirstChild to WaitForChild that yields until it returns the Tool.
	
	plr.CharacterAdded:Connect(function(char)
		local hum = char:FindFirstChildOfClass("Humanoid")
		Tool.Equipped:Connect(function()
			hum.BreakJointsOnDeath = false
			hum.RequiresNeck = false
			Tool.Activated:Connect(function()
				RagdollModule.RagdollCharacter()
				task.wait(5)
				RagdollModule.UnRagdollCharacter()
			end)
		end)
	end)
end)

Okay, I will try that, thanks.

I’m attaching a working code with comments. I understand you’re using a server script to make sure everyone who uses the tool gets ragdolled, even exploiters.

local Players = game:GetService("Players")
--[[ ReplicatedStorage more suitable?
	 ReplicatedFirst is intended for specific use cases, like loading
	 screens and other things that have to be at client's hand right away).
]]
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local RagdollModule = require(ReplicatedFirst.RagdollModule)

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		
		--[[ Only use this line if you're completely sure the tool is
			 going to appear. Backpack is created on character spawn.
			 Clones of the tools are then added. ]]
		local tool = player:WaitForChild("Backpack"):WaitForChild("Tool")
		
		--[[ Avoid nested connections. The previous code was creating
			 .Activated connection on every equip without disconnecting. ]]
		
		-- Consider if .Equipped is even needed.
		tool.Equipped:Connect(function()
			humanoid.BreakJointsOnDeath = false -- Maybe set a single time?
			humanoid.RequiresNeck = false
		end)
		
		-- Fires when tool is clicked while being equipped.
		tool.Activated:Connect(function()
			-- ragdoll here
			-- debounce
			print("OOPS, you fell, too bad...")
		end)
	end)
end)
1 Like

Thanks for help, now I can do ragdoll testing.

Thanks for telling me how to make script better, I will do that as well.

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