Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Script does not destroy StringValue?

Asked by
DogeIXX 172
5 years ago

Hey there. I am creating a script where all players that are in the game get listed. If someone leaves or gets killed from the game the StringValue gets destroyed. Now, I am doing the Player Leave thing, but the script does not destroy the StringValue.

game.Players.PlayerRemoving:Connect(function(player)
    local PlayerFolder = workspace.GameD
    local PlayerName = player.Name
    PlayerFolder:WaitForChild(PlayerName):Destroy()
    local AllPlayer = workspace.AllPlayer
    local PlayerCount = workspace.GameD:GetChildren()
    local NumberA = 0
    for i, user in ipairs(PlayerCount) do
        local CompleteName = "User" .. NumberA
        if AllPlayer:WaitForChild(CompleteName).Value == PlayerName then
        AllPlayer:WaitForChild(CompleteName):Destroy()
        print(CompleteName)
    else
        NumberA = NumberA + 1
    end
    end
    NumberA = 0
end)

When the game starts, every player will get their StringValue. Like "User0", "User1" till User9. The players are counted by created StringValues when they joined.

"Every player gets StringValue at game start"

local PlayerCount = workspace.GameD:GetChildren()
print(PlayerCount)
local NumberC = 0

for i, user in ipairs(PlayerCount) do
    local PlayerFile = Instance.new("StringValue")
    PlayerFile.Name = "User" .. NumberC
    PlayerFile.Parent = workspace.AllPlayer
    PlayerFile.Value = user.Name
    NumberC = NumberC + 1
    print(PlayerFile.Name)
    print(PlayerFile.Value)
end

"Every Player who joins gets StringValue"

game.Players.PlayerAdded:Connect(function(player)
    local PlayerFolder = workspace.GameD
    local Player2 = player
    local PlayerSave = Instance.new("StringValue")
    PlayerSave.Name = Player2.Name
    PlayerSave.Parent = PlayerFolder
end)

1 answer

Log in to vote
0
Answered by
thesit123 509 Moderation Voter
5 years ago
Edited 5 years ago

You're doing too much and you have a lot of unnecessary codes. Here's my fixed code:

-- StringValue when a player joins
game.Players.PlayerAdded:Connect(function(player)
    local PlayerSave = Instance.new("StringValue")
    PlayerSave.Name = player.Name
    PlayerSave.Parent = workspace.GameD
end)


-- StringValue at game start
for userNumber, user in ipairs(workspace.GameD:GetChildren()) do
    local PlayerFile = Instance.new("StringValue")
    PlayerFile.Name = "User" .. userNumber
    PlayerFile.Parent = workspace.AllPlayer
    PlayerFile.Value = user.Name
end


-- Destroy StringValue when player leaving
game.Players.PlayerRemoving:Connect(function(player)
    for i, user in ipairs(workspace.GameD:GetChildren()) do
        if user.Value == player.Name then
            user:Destroy()
        end
    end
    for i, user in ipairs(workspace.AllPlayer:GetChildren()) do
        if user.Value == player.Name then
            user:Destroy()
        end
    end
    -- ^^ Destroy the StringValues if it was found
end)
0
Well it works good so far, but It still does not destroy the "User0" or for example "User5" StringValue. Thats what I try to do. It should check every Values from the StringValues in the Folder and if the Value matches, it gets destroyed. DogeIXX 172 — 5y
0
Fixed it, It should work fine now. thesit123 509 — 5y
0
Thanks, it works fine now! DogeIXX 172 — 5y
Ad

Answer this question