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

Why isn't my function giving all players a gear? [Mostly Solved]

Asked by 5 years ago
Edited 5 years ago

I am making a Minigame like thing, and I am quickly making a test so I can adapt with it. So, I went and made a function that gave all players a sword, and I was going to reverse enginner it to make it removed (im gonna have to figure that out my self), But it wont work.

This is the code I did.

function givesword()
    local player = game.Players:GetPlayers()
    local tool = script.ClassicSword:Clone()
    tool.Parent = player.Backpack
end

If anyone knows what I am doing wrong, that will be grateful. Thanks. :P

Details: The gear is inside the script which runs the main game. The location of the script is inside ServerScriptService. Everything else works, except for giving all players the gear.

Update: So, I got the adding the sword part done, I am just having issue with removing the sword from the players.

function removesword()
    local players = game.Players:GetPlayers()

    for i, player in pairs(players) do
        player.Backpack.ClassicSword:Destroy()
    end
end

This was my attempt. Ended up breaking the entire cycle. oof.

0
Instead of using Backpack,try using StarterPack rochel89 42 — 5y
0
It doesn't work sadly. I think it may have to do with the local 'player', but I am not sure. :P RGamesDev 22 — 5y
0
Do not use StarterPack... the issue is that player is a table. SummerEquinox 643 — 5y
0
Iterate table 'player' and give each index a new copy of 'tool'. SummerEquinox 643 — 5y
View all comments (3 more)
0
if he places the tools in StarterPack people spectating the game will have the tool as well hellmatic 1523 — 5y
0
uhh, i'm an ok scripter, but i dont really understand scripter talk yet. is it alright if someone can explain what is being talked about in a simpler way? thanks. :P RGamesDev 22 — 5y
0
Sorry,my mistake rochel89 42 — 5y

1 answer

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
5 years ago

regarding SummerEquionox's comment Iterate table 'player' and give each index a new copy of 'tool'

function givesword()
    local players = game.Players:GetPlayers() -- GetPlayers() returns a table of players. ex: {player1, player2, player3}

    -- iterate or loop through the players table:

    for i, player in pairs(players) do 
        local tool = script.ClassicSword:Clone()
        tool.Parent = player.Backpack
    end
end
0
Seems really good! I just got to find out how to iterate/loop through the table. :P RGamesDev 22 — 5y
0
Seems like it works! Now I have to figure out how to reverse engineer this script... RGamesDev 22 — 5y
Ad

Answer this question