Script breaking when reseting character

Hello, I am having some trouble with a saw script:

When I reset character I get this error image in this line image

Full script:

local tool = script.Parent
local plr = game.Players.LocalPlayer
local anim = script.Parent:WaitForChild("CutAnim")
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local hey = character:WaitForChild("Humanoid"):LoadAnimation(anim)
local ui = plr.PlayerGui:WaitForChild("SawUI") or script.Parent.SawUI
local text = ui.TextLabel
local mouse = plr:GetMouse()
local event = script.Parent.Event.Value
local particleevent = script.Parent.ParticleEvent.Value
local things = tool.Handle:GetChildren()
local sound = script.Parent.Sound

tool.Activated:Connect(function()
	if mouse.Target.Parent.Name == "Bdoor" and mouse.Target.Anchored == true then
		local hi = mouse.Target.Parent
		print("Good thing")
		if mouse.Target.Parent.Name == "Bdoor" and (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude > 1.5 and (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude < 4 then
			hey:Play()
			sound:Play()
			local on = true
			particleevent:FireServer(things, on)
			wait(4)
			local parts = hi:GetDescendants()
			local on = false
			particleevent:FireServer(things, on)
			hey:Stop()
			event:FireServer(parts)
		elseif (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude < 1.5 then
			text.Text = "You are too close"
			print("Too close")
			wait(1.5)
			text.Text = ""
		elseif (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude > 4 then
			text.Text = "You are too far"
			print("Too far")
			wait(1.5)
			text.Text = ""
		else
			print("This is broken")
		end
	else
		print("Bad thing")
		text.Text = "You cannot break this"
		wait(1.5)
		text.Text = ""
	end
end)

tool.Deactivated:Connect(function()
	hey:Stop()
	sound:Stop()
	local on = false
	particleevent:FireServer(things, on)
end)

2 Likes

Oh that annoying problem. Just put everything under the game.Players.LocalPlayer into a .CharacterAdded event(or maybe just the LoadAnimation).

1 Like

Like this?

local hey = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(anim) or game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild"Humanoid":LoadAnimation(anim)

No I mean something like this:

local player = game.Players.LocalPlayer
player.CharacterAdded:Connect(function(character)
    local hey = character:WaitForChild("Humanoid"):LoadAnimation(Anim)
    --rest of the code
end)
1 Like

Now it says that hey is nil

local tool = script.Parent
local plr = game.Players.LocalPlayer
local anim = script.Parent:WaitForChild("CutAnim")
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
plr.CharacterAdded:Connect(function(char)
	hey = char:WaitForChild("Humanoid"):LoadAnimation(anim)
end)
local ui = plr.PlayerGui:WaitForChild("SawUI") or script.Parent.SawUI
local text = ui.TextLabel
local mouse = plr:GetMouse()
local event = script.Parent.Event.Value
local particleevent = script.Parent.ParticleEvent.Value
local things = tool.Handle:GetChildren()
local sound = script.Parent.Sound

tool.Activated:Connect(function()
	if mouse.Target.Parent.Name == "Bdoor" and mouse.Target.Anchored == true then
		local hi = mouse.Target.Parent
		print("Good thing")
		if mouse.Target.Parent.Name == "Bdoor" and (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude > 1.5 and (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude < 4 then
			hey:Play()
			sound:Play()
			local on = true
			particleevent:FireServer(things, on)
			wait(4)
			local parts = hi:GetDescendants()
			local on = false
			particleevent:FireServer(things, on)
			hey:Stop()
			event:FireServer(parts)
		elseif (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude < 1.5 then
			text.Text = "You are too close"
			print("Too close")
			wait(1.5)
			text.Text = ""
		elseif (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude > 4 then
			text.Text = "You are too far"
			print("Too far")
			wait(1.5)
			text.Text = ""
		else
			print("This is broken")
		end
	else
		print("Bad thing")
		text.Text = "You cannot break this"
		wait(1.5)
		text.Text = ""
	end
end)

tool.Deactivated:Connect(function()
	hey:Stop()
	sound:Stop()
	local on = false
	particleevent:FireServer(things, on)
end)


Oh yeah forgot put everything under the CharacterAdded into the CharacterAdded event.

1 Like

Like this?

local tool = script.Parent
local plr = game.Players.LocalPlayer
local anim = script.Parent:WaitForChild("CutAnim")
local ui = plr.PlayerGui:WaitForChild("SawUI") or script.Parent.SawUI
local text = ui.TextLabel
local mouse = plr:GetMouse()
local event = script.Parent.Event.Value
local particleevent = script.Parent.ParticleEvent.Value
local things = tool.Handle:GetChildren()
local sound = script.Parent.Sound

plr.CharacterAdded:Connect(function(character)
	local hey = character:WaitForChild("Humanoid"):LoadAnimation(anim)
	tool.Activated:Connect(function()
		if mouse.Target.Parent.Name == "Bdoor" and mouse.Target.Anchored == true then
			local hi = mouse.Target.Parent
			print("Good thing")
			if mouse.Target.Parent.Name == "Bdoor" and (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude > 1.5 and (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude < 4 then
				hey:Play()
				sound:Play()
				local on = true
				particleevent:FireServer(things, on)
				wait(4)
				local parts = hi:GetDescendants()
				local on = false
				particleevent:FireServer(things, on)
				hey:Stop()
				event:FireServer(parts)
			elseif (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude < 1.5 then
				text.Text = "You are too close"
				print("Too close")
				wait(1.5)
				text.Text = ""
			elseif (mouse.Target.Position - character.HumanoidRootPart.Position).magnitude > 4 then
				text.Text = "You are too far"
				print("Too far")
				wait(1.5)
				text.Text = ""
			else
				print("This is broken")
			end
		else
			print("Bad thing")
			text.Text = "You cannot break this"
			wait(1.5)
			text.Text = ""
		end
	end)
	
	tool.Deactivated:Connect(function()
		hey:Stop()
		sound:Stop()
		local on = false
		particleevent:FireServer(things, on)
	end)
end)

Yep.
(30charsssssssssssssssss)

Now the whole script is broken.

Are there any errors?(30charss)

no

3000000000000000000000000000000000000

1 Like