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