I need a script which will give me a tool when I join. I found one, but it doesn't work. I want to make sure the gun only goes to me. The issue is the script does not give me the gun.
game.Players.PlayerAdded:Connect(function(plr) local character = game.Workspace:WaitForChild(plr.Name) if plr.Name == "Mycran" then local g = game.ServerStorage.Tool:Clone(HyperlaserGun) g.Parent = plr.Backpack end end)
When you use clone, you should use it as tool:Clone()
, not tool:Clone(Gun)
. Also, your script would only give the tool to the player after joining for the first time. When you reset, the tool will disappear. I have included CharacterAdded
so that it will clone the tool into your backpack each time you respawn.
game:GetService('Players').PlayerAdded:Connect(function(plr) if plr.Name == 'Mycran' then plr.CharacterAdded:Connect(function(char) if char and char:FindFirstChild('Humanoid') then local tool = game:GetService('ServerStorage').HyperlaserGun:Clone() tool.Parent = plr.Backpack end end) end end)
There are a few things wrong with this script and a few things that I wouldn't recommend doing
First thing is that you are getting the Character by looking for an instance with that name in Workspace when you can just get the Character like this Player.Character
but you don't even need the Character name you can just use Player.Name
I would recommend identifying your Player with Player.UserId
though which is what I did below
Second thing is Clone doesn't take any parameters
Third thing is you should use GetService to get Services
Fixed version:
game.Players.PlayerAdded:Connect(function(plr) if plr.UserId == 160153718 then local g = game:GetService("ServerStorage").HyperlaserGun:Clone() g.Parent = plr.Backpack end end)
You can read more about the Player Instance here
Please let me know if you are having any problems with this or if there is something you don't understand :)