Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by
DemGame 106
1 month ago

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.

0
use userinputservice instead of keydown Pupppy44 671 — 1mo
0
Can you please give an example of this through an answer so that I can mark it as an answer? thanks. DemGame 106 — 1mo

1 answer

Log in to vote
1
Answered by 1 month ago

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

0
Thanks. DemGame 106 — 1mo
Ad

Answer this question