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

Whats wrong with this script?

Asked by
Moosee 62
10 years ago

What is wrong with this script? I'm trying to make it so when you click the hat it gives you it on your head, I've been trying for hours, whats wrong?

function onClicked()
--code
if onClicked(PlayerWhoClicked) then
    h = Instance.new("Hat")
        p = Instance.new("Part")
        h.Name = "Headphones"
        p.Parent = h
        p.Position = hit.Parent:findFirstChild("Head").Position
        p.Name = "Handle" 
        p.formFactor = 0
        p.Size = Vector3.new(2, 1, 1) 
        p.BottomSurface = 0 
        p.TopSurface = 0 
        p.Locked = true 
        script.Parent.Mesh:clone().Parent = p
        h.Parent = hit.Parent
        h.AttachmentPos = Vector3.new(0, 0.3, 0)
        wait(5)
        debounce = true
end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

2 answers

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
10 years ago

This code would crash the first time the onClicked function ran because it is calling itself recursively forever. PlayerWhoClicked is also undefined.

Ad
Log in to vote
1
Answered by
spynaz 30
10 years ago

Your code is kind of all messed up. Anyway, I fixed it now so here you go:

local debounce = true

function onClicked(player)
    if debounce then
        debounce = false

        local char = player.Character
            h = Instance.new("Hat")
            p = Instance.new("Part")
            h.Name = "Headphones"
            p.Parent = h
        p.Size = Vector3.new(2, 1, 1) 
            p.Position = CFrame.new(char:findFirstChild("Head").CFrame.p)
            p.Name = "Handle" 
            p.FormFactor = "Smooth"
            p.BottomSurface = "Smooth"
            p.TopSurface =  "Smooth"
            p.Locked = true 
            script.Parent.Mesh:clone().Parent = p
            h.Parent = char
            h.AttachmentPos = Vector3.new(0, 0.3, 0)
            wait(5)
            debounce = true
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)


0
Didn't work. Moosee 62 — 10y
0
Oh I see, on line 13 change it to this: p.CFrame = CFrame.new(char:findFirstChild("Head").CFrame.p) spynaz 30 — 10y
0
Still didn't work. Moosee 62 — 10y

Answer this question