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

If "specific" player enters game, add hat to only them?

Asked by 10 years ago

I've tried making so when a specific player (let's say me) enters the game, a script will put a hat on their head. It's like Ontouch but without touching it. And when you die the script will re-add the hat(s) back onto you. This have been troubling me for weeks and I cannot seem to find the answer, does anyone know of a script that could make this work ? Much obliged if you can figure it out!

I already have this default script I always start from but keep getting nowhere with it.

debounce = true

function onTouched(hit)
    if (hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == true) then
        debounce = false
        h = Instance.new("Hat")
        p = Instance.new("Part")
        h.Name = "CinnamonHair"
        p.Parent = h
        p.Position = hit.Parent:findFirstChild("Head").Position
        p.Name = "Handle" 
        p.formFactor = 0
        p.Size = Vector3.new(1, 2.4, 2) 
        p.BottomSurface = 0 
        p.TopSurface = 0 
        p.Locked = true 
        script.Parent.Mesh:clone().Parent = p
        h.Parent = hit.Parent
                h.AttachmentForward = Vector3.new(-0, -0.0995, -0.995)
        h.AttachmentPos = Vector3.new(0, 0.9, 0)
                h.AttachmentRight = Vector3.new(1, 0, 0)
                h.AttachmentUp = Vector3.new(0, 0.995, -0.0995)
        wait(5)
        debounce = true
    end
end

script.Parent.Touched:connect(onTouched)

I was thinking you could replace "onTouched" with a player detect with a player id or name associated with it. If that's possible?

3 answers

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

thecanadiangamesnerd's answer is frankly wrong and won't work.

The key to this problem is to hook onto the right moment.

The best event I can think of to use is DescendantAdded. This event occurs when something enters the workspace, at any level. We'll specifically look for a "Head" inside of a character of the right name.

We then apply the same hat giving procedure you offered to that model (instead of hit.Parent)

function giveHat(character)
    local h = Instance.new("Hat")
    local p = Instance.new("Part")
    h.Name = "CinnamonHair"
    p.Parent = h
    p.Position = character:FindFirstChild("Head").Position
    p.Name = "Handle" 
    p.formFactor = 0
    p.Size = Vector3.new(1, 2.4, 2) 
    p.BottomSurface = 0 
    p.TopSurface = 0 
    p.Locked = true 
    script.Parent.Mesh:clone().Parent = p
    h.Parent = character
    h.AttachmentForward = Vector3.new(-0, -0.0995, -0.995)
    h.AttachmentPos = Vector3.new(0, 0.9, 0)
    h.AttachmentRight = Vector3.new(1, 0, 0)
    h.AttachmentUp = Vector3.new(0, 0.995, -0.0995)
end

function newObject(obj)
    if obj.Name == "Head" and obj.Parent.Name:lower() == "yourplayernamehere" then
        giveHat(obj.Parent);
    end
end
workspace.DescendantAdded:connect(newObject);
Ad
Log in to vote
-1
Answered by 10 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Hi there.

function onPlayerAdded(newplayer)
    if newplayer.Name == "Person" then
        --Your hat placing stuff goes here.
    end
end
game.Players.PlayerAdded:connect(onPlayerAdded)

Enjoy, and happy scripting!

0
@thecanadiangamesnerd I've tried multiple ways to make this work ingame and it still doesn't work :( EastHalo0 15 — 10y
0
function onPlayerAdded(newplayer) if newplayer.Name == "EastHalo0" then debounce = false h = Instance.new("Hat") p = Instance.new("Part") h.Name = "CinnamonHair" p.Parent = h p.Position = hit.Parent:findFirstChild("Head").Position p.Name = "Handle" p.formFactor = 0 p.Size = Vector3.new(1, 2.4, 2) p.BottomSurface EastHalo0 15 — 10y
0
Replace any "hit.Parent" with "newplayer.Character". That should do it. The only thing that happens currently is the script tries to position the hat over a nonexistant hit.parent. Enjoy! thecanadiangamesnerd 20 — 10y
Log in to vote
-1
Answered by 10 years ago

@thecanadiangamesnerd I've tried multiple ways to make this work ingame and it still doesn't

function onPlayerAdded(newplayer)
    if newplayer.Name == "EastHalo0" then

        debounce = false
        h = Instance.new("Hat")
        p = Instance.new("Part")
        h.Name = "CinnamonHair"
        p.Parent = h
        p.Position = hit.Parent:findFirstChild("Head").Position
        p.Name = "Handle" 
        p.formFactor = 0
        p.Size = Vector3.new(1, 2.4, 2) 
        p.BottomSurface = 0 
        p.TopSurface = 0 
        p.Locked = true 
        script.Parent.Mesh:clone().Parent = p
        h.Parent = hit.Parent
                h.AttachmentForward = Vector3.new(-0, -0.0995, -0.995)
        h.AttachmentPos = Vector3.new(0, 0.9, 0)
                h.AttachmentRight = Vector3.new(1, 0, 0)
                h.AttachmentUp = Vector3.new(0, 0.995, -0.0995)
        wait(5)
        debounce = true

    end
end

game.Players.PlayerAdded:connect(onPlayerAdded)

Answer this question