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 11 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.

01debounce = true
02 
03function onTouched(hit)
04    if (hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == true) then
05        debounce = false
06        h = Instance.new("Hat")
07        p = Instance.new("Part")
08        h.Name = "CinnamonHair"
09        p.Parent = h
10        p.Position = hit.Parent:findFirstChild("Head").Position
11        p.Name = "Handle"
12        p.formFactor = 0
13        p.Size = Vector3.new(1, 2.4, 2)
14        p.BottomSurface = 0
15        p.TopSurface = 0
View all 28 lines...

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

01function giveHat(character)
02    local h = Instance.new("Hat")
03    local p = Instance.new("Part")
04    h.Name = "CinnamonHair"
05    p.Parent = h
06    p.Position = character:FindFirstChild("Head").Position
07    p.Name = "Handle"
08    p.formFactor = 0
09    p.Size = Vector3.new(1, 2.4, 2)
10    p.BottomSurface = 0
11    p.TopSurface = 0
12    p.Locked = true
13    script.Parent.Mesh:clone().Parent = p
14    h.Parent = character
15    h.AttachmentForward = Vector3.new(-0, -0.0995, -0.995)
View all 26 lines...
Ad
Log in to vote
-1
Answered by 11 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.

1function onPlayerAdded(newplayer)
2    if newplayer.Name == "Person" then
3        --Your hat placing stuff goes here.
4    end
5end
6game.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 — 11y
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 — 11y
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 — 11y
Log in to vote
-1
Answered by 11 years ago

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

01function onPlayerAdded(newplayer)
02    if newplayer.Name == "EastHalo0" then
03 
04        debounce = false
05        h = Instance.new("Hat")
06        p = Instance.new("Part")
07        h.Name = "CinnamonHair"
08        p.Parent = h
09        p.Position = hit.Parent:findFirstChild("Head").Position
10        p.Name = "Handle"
11        p.formFactor = 0
12        p.Size = Vector3.new(1, 2.4, 2)
13        p.BottomSurface = 0
14        p.TopSurface = 0
15        p.Locked = true
View all 28 lines...

Answer this question