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

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

Asked by
Mycran 7
4 years ago

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)
0
Are there any errors? Filipalla 504 — 4y

2 answers

Log in to vote
-1
Answered by
Mayk728 855 Moderation Voter
4 years ago
Edited 4 years ago

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)
0
I think I lost a few brain cells when i read "Object:Clone()" although he's already using it properly DeceptiveCaster 3761 — 4y
0
"ServerStorage.Tool:Clone(Hyperlasergun)" doesn't sound right to me. Mayk728 855 — 4y
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" Filipalla 504 — 4y
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) Mayk728 855 — 4y
View all comments (5 more)
0
You should still use FindFirstChildOfClass imo Filipalla 504 — 4y
0
Cus we should teach the newbies the proper way to do things :3 Filipalla 504 — 4y
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. Mayk728 855 — 4y
0
Just because core scripts use FindFirstChild("Humanoid") doesn't mean we shouldn't use FindFirstChildOfClass("Humanoid") Filipalla 504 — 4y
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 Filipalla 504 — 4y
Ad
Log in to vote
0
Answered by
Filipalla 504 Moderation Voter
4 years ago
Edited 4 years ago

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

Answer this question