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

How do I prevent scripts from breaking when the player dies?

I have a local script inside of StarterCharacterScripts, and it only works if the player doesn't die. Any suggestions?

Here is my code:

--do variables
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
repeat wait() until Character:FindFirstChild("Humanoid")
local humanoid = Character.Humanoid
local Mouse = Player:GetMouse()
local debounce = false
local anim = workspace["Cyborg SP1"]
local playAnim = humanoid:LoadAnimation(anim)
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("Cyborg SP1")

--make an animation play when button is pressed
Mouse.KeyDown:connect(function(key)
	if debounce == false then
		if key == "1" then
			print("test")
			debounce = true
			playAnim:Play()
			script.Parent.Parent.Humanoid.WalkSpeed = 0
			event:FireServer()
			wait(0.7)
			script.Parent.Parent.Humanoid.WalkSpeed = 16
			wait(1.3)
			debounce = false
		end
	end
end)
--fire server for projectiles and stuff

Also, I am not sure if this is important or not, but this script is placed inside of a model in StarterCharacterScripts.

2 comments

0

use userinputservice instead of keydown

0

Can you please give an example of this through an answer so that I can mark it as an answer? thanks.

1 answer

✓ Accepted answer

try placing the script in the StarterPlayerScripts object (StarterPlayer.StarterPlayerScripts) considering that you get the Character via the Player, this should work fine.

1 comment

0

Thanks.

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