#I'm new to tables, please help.

1 messages · Page 1 of 1 (latest)

pseudo meadow
#

So I am practicing on tables or arrays, I don't really know what they're called; anywho, why does the chosen player not get the sword?

Script:

local serverStorage = game:GetService("ServerStorage")
local plrs = game:GetService("Players")

local Sword = serverStorage:WaitForChild("Sword")

local waitListTable = {}

plrs.PlayerAdded:Connect(function(plr)
table.insert(waitListTable, plr)

print(waitListTable)

if #waitListTable == 3 then
    local imposterIndex = math.random(1, #waitListTable)
    local Imposter = waitListTable[imposterIndex]
    
    local clonedSword = Sword:Clone()
    local Backpack = Imposter:WaitForChild("Backpack")
    
    
    print(Imposter.Name.." is the Imposter!")
    
    clonedSword.Parent = Backpack
    
    --[[if Imposter == nil then
        print("he left")
    end]]
end

end)

sick ferry
#

Hmm maybe add a wait for the character to load before giving the

#

Sword

fresh plinth
#

are you testing alone?

spiral elbow
#

ait

spiral elbow
#

Wait

#

I might have misread lol

pseudo meadow
inland fernBOT
#

studio** You are now Level 6! **studio

bleak sleet
#

nothing?

fresh plinth
#

meaning?

pseudo meadow
pseudo meadow
fresh plinth
#

you are checking if there 3 people

bleak sleet
#

^

fresh plinth
#

but its only you

spiral elbow
#

Yeah, I don't see a problem here. When the third player joins, it should pick a random person and give them it

pseudo meadow
bleak sleet
#

So you got 3 people?

pseudo meadow
#

i guess its because the backpack does not load fast enough

fresh plinth
#

did you get 3 people or not

#

answer that

pseudo meadow
#

yes

bleak sleet
pseudo meadow
#

there is

bleak sleet
#

so it does load

fresh plinth
#

oh

pseudo meadow
#

its just all me

bleak sleet
#

unless you get an warning

fresh plinth
bleak sleet
#

What exacly is tool?

fresh plinth
#

alt accounts?

bleak sleet
#

Are you sure its valid tool

pseudo meadow
#

yes i checked the name

bleak sleet
#

Show ss of output

#

to be safe

#

And ss of explorer

pseudo meadow
bleak sleet
#

with player backpack visible

fresh plinth
#

oh

spiral elbow
pseudo meadow
#

ok so i wait for the player character to load?

bleak sleet
#

youre waiting for a child

#

So it shouldnt be a problem

#

can you screenshare rq

sick ferry
pseudo meadow
pseudo meadow
spiral elbow
# bleak sleet So it shouldnt be a problem

Normally it wouldn't be a problem, but roblox is dumb and actually creates an initial backpack object, and then deletes it to spawn another when a character is created.

bleak sleet
bleak sleet
pseudo meadow
#

i found this

bleak sleet
#

So if it was tested few times it must have at least selected loaded one once

spiral elbow
#

As long as it picks any of the other two, it would work in that case

fresh plinth
#

is imposter a team

bleak sleet
#

@pseudo meadow can you just screenshare in vc

inland fernBOT
#

studio** You are now Level 8! **studio

pseudo meadow
bleak sleet
#

everything

sick ferry
#

Dev guy just try it

#

The wait

fresh plinth
#

ss of explorer

pseudo meadow
#

Imposter.CharacterAdded:Wait()

#

oh it work now

#

local serverStorage = game:GetService("ServerStorage")
local plrs = game:GetService("Players")

local Sword = serverStorage:WaitForChild("Sword")

local waitListTable = {}

plrs.PlayerAdded:Connect(function(plr)
table.insert(waitListTable, plr)

print(waitListTable)

if #waitListTable == 3 then
    local imposterIndex = math.random(1, #waitListTable)
    local Imposter = waitListTable[imposterIndex]
    
    Imposter.CharacterAdded:Wait()
    
    local clonedSword = Sword:Clone()
    local Backpack = Imposter:WaitForChild("Backpack")
    
    print(Imposter.Name.." is the Imposter!")
    
    clonedSword.Parent = Backpack
end

end)

bleak sleet
#

how.

pseudo meadow
#

i added the wait

sick ferry
#

Exactly

pseudo meadow
#

to character added

sick ferry
#

lol

bleak sleet
#

How the hell does that change anything huh

sick ferry
bleak sleet
#

Youre waiting for backpack either way

sick ferry
#

Way to quick

pseudo meadow
#

i think it waits for the character to load then it checks for the backpack

bleak sleet
pseudo meadow
#

idk erik is the proffessional here i just followed what he said

sick ferry
#

And still fs it up

pseudo meadow
#

well anyways thanks

sick ferry
#

Yes

spiral elbow
bleak sleet
#

welp at least it works

#

how? idk

sick ferry
#

lol

inland fernBOT
#

studio** You are now Level 1! **studio

pseudo meadow
#

if the logic is always going to be like this idk how im going to become decent at coding

#

anyways thanks cya

sick ferry
#

You’ll learn more and more

#

That u struggle

spiral elbow
# bleak sleet how? idk

Well. When a player object is created, an initial backpack object is created with it. BUT, whenever a new character is created, a new backpack object is created and the old one is deleted. That means that when you check for the backpack on a player join, it'll find the initial backpack that gets immediately deleted upon the character's spawn unless you make sure it does otherwise

#

BUT, there is problem with the solution he just put

sick ferry
#

Type shi

spiral elbow
#

If he just does player.CharacterAdded:Wait(), this won't work outside of studio's environment.

#

It's currently working because all three players are being added in before any characters are, but since in an actual game at least two of those characters are already in, player.CharacterAdded:Wait() will stop the script if it picks either of those two initial characters, since it'll be waiting for a new character.

#

@pseudo meadow
change

Imposter.CharacterAdded:Wait()

to

local character = Imposter.Character or Imposter.CharacterAdded:Wait()

To avoid the problem I stated above