Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is causing local scripts to break on player death/respawn?

Asked by 4 years ago

I am using multiple local scripts in my game to get player input and give the player an immediate response to certain actions, such as hiding in bushes or using abilities. However, after the player dies once in a live server, both of these scripts break, causing the player to only move and preventing them from going invisible or using their dodge roll ability, and after making many changes to the code I still do not understand where this problem is being caused in these scripts or how to fix it. Can anyone help tell me why? Here is the code that handles these events in case it helps: Variables / object identification

local Players = game:getService("Players")
local Player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = script.Parent
local root = player:WaitForChild("HumanoidRootPart")
local notInBush = player:WaitForChild("notInBush")
local isShooting = player:WaitForChild("isShooting")
local isHit = player:WaitForChild("isHit")

Player.CharacterAdded:Connect(function(Char)
    player = Char
    root = Char:WaitForChild("HumanoidRootPart")
    notInBush = player:WaitForChild("notInBush")
    isShooting = player:WaitForChild("isShooting")
    isHit = player:WaitForChild("isHit")
end)

Invisibility collision handling

function hideInBush()
    local head = player:FindFirstChild("Head")
    local face = head:FindFirstChild("face")
    for _, object in ipairs(player:GetChildren()) do
        if object:IsA("Accessory") then
            object.Handle.Transparency = 1
        elseif object:IsA("BasePart") and not (object.Name == "HumanoidRootPart") then
            object.Transparency = 1
        end
    end
    face.Transparency = 1
    hideInBushEvent:FireServer(player)
    notInBush.Value = false
end

function leaveBush()
    local head = player:FindFirstChild("Head")
    local face = head:FindFirstChild("face")
    for _, object in ipairs(player:GetChildren()) do
        if object:IsA("Accessory") then
            object.Handle.Transparency = 0
        elseif object:IsA("BasePart") and not (object.Name == "HumanoidRootPart") then
            object.Transparency = 0
        end
    end
    face.Transparency = 0
    leaveBushEvent:FireServer(player)
    notInBush.Value = true
end

Roll ability

function dodgeRoll(actionName, userInputState, inputObject)
    local cameraCFrame = workspace.CurrentCamera.CFrame
    if userInputState == Enum.UserInputState.Begin and canShootAbility and notShootingAbility == true then
        abilityCharges = abilityCharges - 1
        abilityAmmoUpdateEvent:FireServer(abilityCharges)
        notShootingAbility = false
        local dodgeInvoke = dodgeRollEvent:InvokeServer(movementKeys[0], movementKeys[2], movementKeys[3], movementKeys[1], cameraCFrame)
        notShootingAbility = true
    end
end

Thanks in advance

0
Where is the script actually located? To me it look's as if it is inside the Character. aiza_teizuki 15 — 4y
0
Sorry, probably should have mentioned that. These scripts are in StarterCharacterScripts. robox243 2 — 4y
0
Isn't the script being deleted after the character dies then? Seeing as when you die your character is replaced. aiza_teizuki 15 — 4y
0
Is there a better location for these types of scripts? robox243 2 — 4y

1 answer

Log in to vote
0
Answered by
megukoo 877 Moderation Voter
4 years ago

As I see, you set player to the Char variable. Normally this isn't bad, but as this in StarterCharacterScripts like you said, the LocalScript won't pick up CharacterAdded because it is placed in after the character loads.

A fix to this could be placing it in StarterPlayerScripts, where on each respawn the variables could be reset properly.

Let me know if this works.

0
Moving the local scripts from StarterCharacter to StarterPlayer definately fixed the problems with the scripts breaking after death, and it also exposed some other problems with the way I am tracking values which aren't too dificult to fix as well. Thank you very much for the help. robox243 2 — 4y
Ad

Answer this question