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

Weapon randomizer script help? [Unsolved]

Asked by 10 years ago

Hey there its me again having problems with a script, the script needs to give players a different sword, but its not working what did I did wrong? also I tried the output window but nothing appeared, and With a server with players of course.

Wait(5)
game.Players.PlayerAdded:connect(function(p)
    local tools = {game.ServerStorage.Sword:Clone(), game.ServerStorage.Crowbar:Clone(), }
    local randTool = tools[math.random(#tools)]
    if p:FindFirstChild("BackPack") then --backpack isnt part of character it's in the player
        randTool.Parent = p.BackPack
    end

end)

2 answers

Log in to vote
-1
Answered by
Dominical 215 Moderation Voter
10 years ago

Characters are models.. so they can't have a Backpack, just the player's data can(game.Players.PLAYERNAME) Yah know what I'm talkin bout? This will help, just use this block.

You got a syntax error because BackPack isn't a member of anything. It's not BackPack, it's Backpack. And also, Backpacks aren't located in Player's character models.

swrds = {game.Lighting.LinkedSword,game.Lighting.Crowbar,game.Lighting.Firebrand} -- Put locations of swords in here that are in ServerStorage or Lighting, or Debris where-ever it is.

for _,v in pairs(game.Players:GetChildren()) do
local sword = swrds[math.random(#swrds)]:Clone()

sword.Parent = v.Backpack -- Change Backpack to StarterGear if you want them to respawn with it.

end
0
Thanks, yes I think I get your explanation thanks a lot! biocommand 30 — 10y
0
It´s not working biocommand 30 — 10y
Ad
Log in to vote
-1
Answered by
m0rgoth 75
10 years ago

@Diminical You got the part about the error correct, but he never tried to put anything into the character in the first place. Also, he wants this to run whenever a player joins the game, so that for loop will just screw things up.

Now, there are 2 problems with your script, biocommand. First is the problem that Diminical outlined: a spelling error. You can resolve this by simply replacing 'BackPack' with 'Backpack'.

The other problem is with your if statement. You check, and rightly so, if the player has its backpack yet. This is necessary because sometimes the backpack will not have loaded yet. If it has, then it will give the player the tool, like it should. However, what if they don't have it yet? Well, the script considers its job done and stops right there. The player will never get their tool. How do we fix this? We have to wait until the player gets their backpack. How do we do this? Well, there is a very simple method in roblox that does this for us, and was intended for this very purpose. Here is how to use it:

local backpack = p:WaitForChild("Backpack")
randTool.Parent = backpack

As you can see, we get the backpack, whether it is loaded or not, and then we set the tool's parent to it.

Answer this question