Script setting backpack's CoreGui doesn't work on respawn

I don’t know why this script does not work on respawn, and I even used Plr.Character instead of character variable.

Script here:

wait()
local Plr = game.Players.LocalPlayer
local StarterGui = game:GetService("StarterGui")
local Lighting = game:GetService("Lighting")

Plr.Character.Ragdolled:GetPropertyChangedSignal("Value"):Connect(function()
	if Plr.Character.Ragdolled.Value == true then
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	else
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
	end
end)

Plr.CharacterAdded:Connect(function()
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
end)

Thanks for any help.

1 Like

First off wait() is deprecated, task.wait() is the new way now.

Also just use the event Humanoid.Died.

1 Like

You can be ragdolled while living, though.

Hmmmm. Try using GetAttribute() then. You can check if the humanoid’s health is 0.

Instead of checking if player has respawned in local script, you can do it in server script and with the remote update backpack

OR
you can check if humanoid died and then add the

task.wait(respawntime)
--- do enabling here

Try that …

Plr.CharacterAdded:Connect(function()
    task.wait(0.33) -- or longer now that you have the player this far
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
end)

Since this got bumped, I might as well deliver a more reliable solution than the one above. Here:

local starterGui = game:GetService('StarterGui')
local players = game:GetService('Players')

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Ragdolled = character:WaitForChild('Ragdolled')

local success, response

------------------------------------------------

local function SetCoreGuiEnabled(element)
	starterGui:SetCoreGuiEnabled(element, true)
end

local function SetCoreGuiDisabled(element)
	starterGui:SetCoreGuiEnabled(element, false)
end

------------------------------------------------

while not success do
	task.wait()
	success, response = pcall(SetCoreGuiEnabled, Enum.CoreGuiType.Backpack)
end

Ragdolled:GetPropertyChangedSignal('Value'):Connect(function()
	if character.Ragdolled.Value then
		SetCoreGuiDisabled(Enum.CoreGuiType.Backpack)
	else
		SetCoreGuiEnabled(Enum.CoreGuiType.Backpack)
	end
end)

Basically, this code uses pcalls, which detect errors and prevent the code from stopping, so we use them to repeatedly set the backpack to enabled. This might cause a couple of errors, but if they do, you should still be good to go.

The reason why I made this is because simply waiting for a few seconds might not play well with latency or the player’s device power. This will always work, instead of sometimes.