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

How Do I make a hat that spawns to people who are in a group?

Asked by
duckyo01 120
10 years ago

I really need help I guess I'll give it a try. :I

local groupId = 1087177
function RegisterPlayer( player ) 
if CheckInGroup(player, groupId) then 
HoldThreadForCharacter(player) 
local hat = 1309911
end 
end 
function CheckInGroup( player, id ) 
if player:IsInGroup(id) then 
return true 
end 
return false 
end 
function HoldThreadForCharacter( player ) 
repeat wait() until Workspace:FindFirstChild(player.Name) 
end 
Game.Players.PlayerAdded:connect(RegisterPlayer) 
for i,v in pairs(Game.Players:GetPlayers()) do 
RegisterPlayer(v) 
end 
game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        asset = game:GetService("InsertService"):LoadAsset(hat)
        give = game:GetService("InsertService"):Insert(asset)
        local children = asset:GetChildren()
        for i = 1, #children do
            children[i].Parent = character
            end
    end)
end)

500$ it's wrong please help me...

1 answer

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
10 years ago

Your indenting style and all of those seemingly useless functions are really confusing me.

Line 15 will error out - :FindFirstChild() tries to FIND the given child in a given object. Returns nil if it doesn't.

Line 23 will error out - hat will not be found anywhere. You might have the script intend to find it in line 5, but, unfortunately, it can only be accessed in the "if" statement in the RegisterPlayer() function since it is a local variable.

Notice that there are two links in "local."

What you wanted is a relatively easy feat to do, and it can be accomplished by around 12 lines or so.

Ignoring all of those functions above line 21, you might need to add the hat spawning code to let people receive their hat when they first get in the game. To do so, you have to put the code in between line 21 and line 22 in your case.

game.Players.PlayerAdded:connect(function (player)
    -- DO STUFF. If the character of the player is being manipulated, use 'player.Character.'
    player.CharacterAdded:connect(function (character)

The reason why you have to have some code before line 22 in order for it to work is because the .CharacterAdded event will not fire until after the first character is dead. I don't know why this happens. But it does.


:IsInGroup: Checks if the player is in a group. Returns true if it is. Accepts a set of integers that represents the group's ID.

EXAMPLE:

if game.Players.LocalPlayer:IsInGroup(7) then
    print("true")
else
    print("false")
end

game:GetService("InsertService"):LoadAsset(): Loads the given asset with a valid ID.

EXAMPLE:

local Item = game:GetService("InsertService"):LoadAsset(23220518)
Item.Parent = workspace

Now, all you have to do is try and utilize the two special terms I just mentioned.

Then try and replicate the code so that it is in two places:

  • Between lines 21 and 22 (with respect to your code).

  • In the anonymous function scope with the .CharacterAdded event.

Try and use the examples to help you in your scripting process.


NOTE:

To have characters wear a hat, just parent the hat object with the Handle basepart to the character itself. You don't have to do any special welding thing, a giver or anything like that.

0
i dont understand @ Redbullusa duckyo01 120 — 10y
0
What do you not understand? Redbullusa 1580 — 10y
Ad

Answer this question