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.

01game.Players.PlayerRemoving:Connect(function(player)
02    local PlayerFolder = workspace.GameD
03    local PlayerName = player.Name
04    PlayerFolder:WaitForChild(PlayerName):Destroy()
05    local AllPlayer = workspace.AllPlayer
06    local PlayerCount = workspace.GameD:GetChildren()
07    local NumberA = 0
08    for i, user in ipairs(PlayerCount) do
09        local CompleteName = "User" .. NumberA
10        if AllPlayer:WaitForChild(CompleteName).Value == PlayerName then
11        AllPlayer:WaitForChild(CompleteName):Destroy()
12        print(CompleteName)
13    else
14        NumberA = NumberA + 1
15    end
16    end
17    NumberA = 0
18end)

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"

01local PlayerCount = workspace.GameD:GetChildren()
02print(PlayerCount)
03local NumberC = 0
04 
05for i, user in ipairs(PlayerCount) do
06    local PlayerFile = Instance.new("StringValue")
07    PlayerFile.Name = "User" .. NumberC
08    PlayerFile.Parent = workspace.AllPlayer
09    PlayerFile.Value = user.Name
10    NumberC = NumberC + 1
11    print(PlayerFile.Name)
12    print(PlayerFile.Value)
13end

"Every Player who joins gets StringValue"

1game.Players.PlayerAdded:Connect(function(player)
2    local PlayerFolder = workspace.GameD
3    local Player2 = player
4    local PlayerSave = Instance.new("StringValue")
5    PlayerSave.Name = Player2.Name
6    PlayerSave.Parent = PlayerFolder
7end)

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:

01-- StringValue when a player joins
02game.Players.PlayerAdded:Connect(function(player)
03    local PlayerSave = Instance.new("StringValue")
04    PlayerSave.Name = player.Name
05    PlayerSave.Parent = workspace.GameD
06end)
07 
08 
09-- StringValue at game start
10for userNumber, user in ipairs(workspace.GameD:GetChildren()) do
11    local PlayerFile = Instance.new("StringValue")
12    PlayerFile.Name = "User" .. userNumber
13    PlayerFile.Parent = workspace.AllPlayer
14    PlayerFile.Value = user.Name
15end
View all 31 lines...
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