Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I need help with a script which will give me (not anyone else) a tool when I join. (?)

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)

1 comment

0

Are there any errors?

2 answers

✓ Accepted answer

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)

9 comments

0

I think I lost a few brain cells when i read "Object:Clone()" although he's already using it properly

0

"ServerStorage.Tool:Clone(Hyperlasergun)" doesn't sound right to me.

0

(Unrelated to Clone stuffs) I would recommend using char:FindFirstChildOfClass("Humanoid") instead of char:FindFirstChild('Humanoid') as a Humanoid doesn't have to be named "Humanoid"

0

well, for a Player, it does have to be named "Humanoid" because some core scripts look for "Humanoid". If it were to be renamed, those core player scripts would break. (example: you wouldn't be able to reset)

0

You should still use FindFirstChildOfClass imo

0

Cus we should teach the newbies the proper way to do things :3

0

while it works, i wouldn't call it proper, because the character instance only names it "Humanoid", and once again would break scripts if it were to be renamed.

0

Just because core scripts use FindFirstChild("Humanoid") doesn't mean we shouldn't use FindFirstChildOfClass("Humanoid")

0

Also, keep in mind OP seems to be a newbie and might learn this way to do it and might at some point use it to say get the Humanoid of an NPC

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 :)

This discussion is preserved as it was. New answers, comments, and votes are closed.