Need help with some code

So, I have a script that checks if the player has a StringValue Called ‘SurvivalTag’ If the player has it, it makes a gui invisible. But if they don’t have it the gui is visible but it is just not working.

Just so you know, I am new to roblox coding and I have no idea on how to fix this.
Hoping I can get a fix :smile:

for _,player in pairs(game.Players:GetPlayers()) do
	for i,v in ipairs(player.Character:GetDescendants()) do
		if v.Name == 'SurvivalTag' and v:IsA('StringValue') then
			player.PlayerGui.MainGui.TpToMap.Visible=false
			player.PlayerGui.MainGui.TpToMap.Active=false
		elseif not player.Character:FindFirstChild('SurvivalTag') then
			player.PlayerGui.MainGui.TpToMap.Visible = true
			player.PlayerGui.MainGui.TpToMap.Active = true
		end
	end
end
1 Like

This code is probably just running once, hence why it doesn’t update when the SurvivalTag does get changed.

Should I try putting a while true loop?

Never mind. That doesn’t work.

Are you trying to detect if the SurvivalTag gets changed, to then change the GUI visibility?


Looks like you’re trying to find if a StringValue even exists in the character…?

Basically, my other main script gives a player a survival tag when the round starts. This code is just to give the player a gui if they dont have the SurvivalTag

Are you trying to detect if a SurvivalTag exists in the character at all? Like, in the character’s hierarchy?

Something that I might be doing wrong is that this is in serverscriptservice. probably a dumb idea but I already tried this in a normal localscript in a gui and it didnt work

Looking at the script, it simply does just look like you’re trying to check if a SurvivalTag exists in the character.

You can use the ChildAdded event to do this:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        
        character.ChildAdded:Connect(function(child)
            if child.Name == "SurvivalTag" then --Change the conditions
                print("added survival tag")
            end
        end)
        
        character.ChildRemoving:Connect(function(child)
            if child.Name == "SurvivalTag" then --Change the conditions
                print("removed survival tag")
            end
        end)
        
    end)
end)

So, if the player dies and they lose the SurvivalTag, will it still be checking?
edit: It works. Thank you!

The problem can be easily solved. I think the problem is the second for loop.
It loops over every descendants of the character, if it found the value it shows the gui, but after that it continues to loop with other descendants, which are not the value and makes the gui re-disappear. To fix it, I recommend doing

local hasValue = false
for _,player in game.Players:GetPlayers() do
    for _,v in player.Character:GetDescendants() do
        if v.Name == 'SurvivalTag' and v:IsA('StringValue') then
            hasValue = true
        end
    end
end
if hasValue then
    --hide gui--
else
    --show gui--
end

You can put this in a function and run it every time you want to check.
Actually it’s recommended to store those values in the player instance, but not the character.
Hope this helps.
Well I mean @DiamondDrencher1 method can also work and is quite simpler.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.