Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why are items duping when the player gets back up?

I'm making a ragdoll script and when the player's health gets low it works fine but when the game is returning the item you get double the weapons. This is a local script under StarterCharacterScripts:

local char = script.Parent
local hum = char.Humanoid

local ragDolled = false

hum.HealthChanged:Connect(function()
	if hum.Health <= 25 and ragDolled == false then
		
		ragDolled = true
		hum.WalkSpeed = 0
		hum.JumpPower = 0
		local setEnabled = hum:GetState() ~= Enum.HumanoidStateType.Physics
		hum:ChangeState(setEnabled and Enum.HumanoidStateType.Physics or Enum.HumanoidStateType.GettingUp)	
		char.Animate.Disabled = setEnabled
		game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame * CFrame.Angles(math.rad(90),0,0)
		hum:UnequipTools()
		
		local tools = game.Players.LocalPlayer.Backpack:GetChildren()
			for i, v in pairs(tools) do
				v.Parent = game.ReplicatedStorage.Tools
					
	
		
		if setEnabled then
			for _,v in pairs(hum:GetPlayingAnimationTracks()) do
				v:Stop(0)
		
				 
					
				end
			end
		end
	end
	if hum.Health >= 35 and ragDolled == true then
		ragDolled = false
		hum.WalkSpeed = 16
		hum.JumpPower = 28
			local tools = game.ReplicatedStorage.Tools:GetChildren()
				for i, v in pairs(tools) do
				v.Parent = game.Players.LocalPlayer.Backpack
					hum:ChangeState(Enum.HumanoidStateType.GettingUp)
						char.Animate.Disabled = false
		end
	end
end)



Also the guns only dupe in game not in studio.

1 answer

I managed to fix it with plenty of debugging and came up with this solution:

local char = script.Parent
local hum = char.Humanoid

local ragDolled = false

hum.HealthChanged:Connect(function()
	if hum.Health <= 25 and ragDolled == false then
		
		ragDolled = true
		hum.WalkSpeed = 0
		hum.JumpPower = 0
		local setEnabled = hum:GetState() ~= Enum.HumanoidStateType.Physics
		hum:ChangeState(setEnabled and Enum.HumanoidStateType.Physics or Enum.HumanoidStateType.GettingUp)	
		char.Animate.Disabled = setEnabled
		game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame * CFrame.Angles(math.rad(90),0,0)
		hum:UnequipTools()
		
		
		local tools = game.Players.LocalPlayer.Backpack:GetChildren()
			for i, v in pairs(tools) do
				v.Parent = game.ReplicatedStorage.Tools
					
	
		
		if setEnabled then
			for _,v in pairs(hum:GetPlayingAnimationTracks()) do
				v:Stop(0)
		
				 
					
				end
			end
		end
	end
	if hum.Health >= 35 and ragDolled == true then
		ragDolled = false
		hum.WalkSpeed = 16
		hum.JumpPower = 28
			local tools = game.ReplicatedStorage.Tools:GetChildren()
				for i, v in pairs(tools) do
				v.Parent = game.Players.LocalPlayer.Backpack
					hum:ChangeState(Enum.HumanoidStateType.GettingUp)
						char.Animate.Disabled = false
		end
	end
end)

hum.Died:Connect(function()
	local tools = game.ReplicatedStorage.Tools:GetChildren()
				for i, v in pairs(tools) do
				v:Destroy()
				end
end)

I just removed the items stored in replicated storage when the player dies

This discussion is preserved as it was. New answers, comments, and votes are closed.