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
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.