Local script breaks after character reset

My punch script doesn’t work after resetting character.
What’s wrong and how can I fix?
It’s also local script and inside of StarterPlayerScripts

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local motion = script.Animation
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
repeat wait() until character:FindFirstChild("Humanoid")
local humanoid = character.Humanoid


UserInputService.InputBegan:Connect(function(Input, gameProcessed)
	if Input.KeyCode == Enum.KeyCode.E and player.frozen.Value == false and player.holdsword.Value == false and not gameProcessed then
		if player.stunned.Value == false then
			if player.hitct.Value == false then
				if player.hitable.Value == false then
					local settruect = ReplicatedStorage:WaitForChild("Settruect")
					settruect:FireServer()
					local domotion = humanoid:LoadAnimation(motion)
					domotion:Play()
					local punch = ReplicatedStorage:WaitForChild("Punch")
					punch:FireServer(player)
					wait(0.5)
					local cnghitable = ReplicatedStorage:WaitForChild("Cnghitable")
					cnghitable:FireServer(player)
					wait(0.2)
					local setfalsect = ReplicatedStorage:WaitForChild("Setfalsect")
					setfalsect:FireServer()
				end
			end
		end
	end
end)
2 Likes

Hope this forum help you after you read it,

Sorry for giving you a link instead of explaining, because I’m bad at explaining. Sorry.

1 Like

I think what he means is you could put this in StarterPlayerScripts, and the old script will be destroyed and a new script will be cloned into the character. The issue here I believe is that once they die, the script is accessing the old character and humanoid, so the animations don’t play on the character that just respawned.

1 Like

Uh no, I meant you could put your script in StarterGui or Starterpack, but if you’re gonna not to doing that, you could use ContextActionService | Documentation - Roblox Creator Hub as the forum mentioned. I don’t know much about this but this might help you.

Try replacing this

local character = player.Character or player.CharacterAdded:Wait()
repeat wait() until character:FindFirstChild("Humanoid")
local humanoid = character.Humanoid

for this

local character = player.Character
local humanoid = character and character.Humanoid
player.CharacterAdded:Connect(function (char)
	character = char
	humanoid = char:WaitForChild("Humanoid")
end)
2 Likes