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

What is wrong with this script?

Asked by 8 years ago
local Tool = game.ServerStorage.Tool
game.Players.PlayerAdded:connect(function(player)
    print(player.Name .. " has joined the game! :D")
    Tool:Clone()
    Tool.Parent = player.Backpack
end)

It is meant to put a tool in the Player's Backpack when the enter.

0
You are not parenting the cloned tool you are parenting the actual tool so this will only work once, try doing Tool:Clone().Parent = player.Backpack Ryzox 220 — 8y

2 answers

Log in to vote
1
Answered by 8 years ago

[[wiki.roblox.com/index.php?title=PlayerAdded]]

Basically,You need to add an onPlayerAdded function since the script is added to the game after the player is added

local Players = game:GetService("Players")

function onPlayerAdded(player)
     print(player.Name .. " has entered the game")
end

--When a player joins, call the onPlayerAdded function
Players.PlayerAdded:connect(onPlayerAdded)

--Call onPlayerAdded for each player already in the game
for _,player in pairs(Players:GetPlayers()) do
     onPlayerAdded(player)
end
Ad
Log in to vote
0
Answered by 8 years ago
local Sword = game.ServerStorage:WaitForChild("Tool")

function onAdded(player)
    print(player.Name .. " has joined the game! :D")
    Sword:Clone()
    Sword.Parent = player:WaitForChild("Backpack")
end

game.Players.PlayerAdded:connect(onAdded)

For some reason you cant name it tool, so name it something like Sword or AK47 and also its a good practice to use :WaitForChild, it will yeild(wait/stop) the script until that object is not nil and it will prevent errors, another good practice is using functions, it makes it so you dont half to copy and paste code over and over instead you can just call the function.

Answer this question