Script works unless player dies

Hi, please try to help me fix my script.

I’m trying to change the EggImage colour depending on whether the egg value is equal to true or false.

This is in StarterGui:
InsideStarterGui

This is in ServerStorage: (there are more BoolValues but I didn’t fit them in the pic)
InsideServerStorage

This is my script in ServerScriptService:

local sStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local player = game:GetService("Players"):WaitForChild(char.Name)
		local eggsFolder = sStorage:WaitForChild("DiscoveredEggs-"..player.Name)
		for i, egg in pairs(eggsFolder:GetChildren()) do
			local eggName = tostring(egg)
			if egg.Value == true then
				player.PlayerGui:WaitForChild("DiscoveredEggs").Frame.ScrollingFrame:WaitForChild(eggName).EggImage.ImageColor3 = Color3.fromRGB(255,255,255)
			else
				player.PlayerGui:WaitForChild("DiscoveredEggs").Frame.ScrollingFrame:WaitForChild(eggName).EggImage.ImageColor3 = Color3.fromRGB(0,0,0)
			end
		end
	end)
end)

Ask as many questions as you’d like, any help would be appreciated!

1- You don’t need this line :
local player = game:GetService("Players"):WaitForChild(char.Name)
you already have player [argument inside the PlayerAdded event].

2- Does the UI disappear or what exactly happens when the player dies?

1 Like

The images change to the colour I want when the player joins. After they die, all the images are the default colour (255,255,255).

There are many ways to solve this, one of them is by making a loop or an event that’ll make sure the colors update even if they died.

Another way is to use any helping variable [ for e.g: boolean]

Make sure ‘Reset on Spawn’ is true.

1 Like
1 Like

I think a different methodology would be in order., why not just have the boolean folder inside replicated storage for client access through local script (local script parented to the GUI)? This would be easier for you since you can just write the for loop within the local and it will execute every time the player spawns

2 Likes

Thanks for this, I’ve set reset on spawn to false & it seems to be working.

well the code doesn’t execute twice as you told us, what that property did was prevent the GUI element from resetting when dying, so it kept the changes from when the code first changed it.

“script works unless player dies” ~~ code doesnt execute twice

Thanks for the information, although I don’t remember saying ‘the code executes twice’. I need the GUI to not reset when the player does so I think the property set to false works for what I need.

Sorry if I didn’t write the topic name accurately but, according to me, everything was working until the player died so I think the name is correct.

Not a solution, just a critique on some of your coding to help you improve.

This can be misleading and is also invoking a function when you already have access to the name of the egg with the Instance.Name property. Try using this instead:

local eggName = egg.Name

This is unnecessarily long when you are essentially doing the same exact thing in either scenario. The first improvement you can implement to make your code more readable is use variables more. This:

Can be made more readable by storing the scrolling frame in which you are displaying the eggs as a variable and then searching it for the egg.

local player = game:GetService("Players"):WaitForChild(char.Name)
local eggsFolder = sStorage:WaitForChild("DiscoveredEggs-"..player.Name)
local eggsFrame = player.PlayerGui:WaitForChild("DiscoveredEggs").Frame.ScrollingFrame

-- for loop

eggsFrame:WaitForChild(egg.Name).EggImage.ImageColor3

We are almost done, but the if-else statement you are using takes up space when the only thing you are changing is the value you are setting the image color to. You can accomplish the same thing with less lines using the Luau implementation of a ternary operator.

eggsFrame:WaitForChild(egg.Name).EggImage.ImageColor3 = if egg.Value then Color3.new(1,1,1) else Color3.new(0,0,0)


Anyways, that takes care of everything that initially caught my eye. Here’s what the completed code should look like. Not all of it, mind you, just the interior portion.

local player = game:GetService("Players"):WaitForChild(char.Name)
local eggsFolder = sStorage:WaitForChild("DiscoveredEggs-"..player.Name)
local eggsFrame = player.PlayerGui:WaitForChild("DiscoveredEggs").Frame.ScrollingFrame

for i, egg in pairs(eggsFolder:GetChildren()) do
	eggsFrame:WaitForChild(egg.Name).EggImage.ImageColor3 = if egg.Value then Color3.new(1,1,1) else Color3.new(0,0,0)
end

Other than that keep up the good work. It may seem like I’m unnecessarily nitpicking things for the sake of it, but writing cleaner and more concise code makes it easier to read and can at times (like in this example) be more efficient. It also makes it easier for others to read and understand if you are working on a team. I also added a space in-between the for loop and the variable assignments. This just helps make the code a bit more readable and is subjective to preference. Anyways, have a good day!

1 Like

This is very helpful, thank you.