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

Script won't do anything?

Asked by 8 years ago

Sorry for all the help needed today guys.

I'm making a coin pusher. When someone clicks the insert button (part with a click-detector) it costs them 1 'coin' and the coin is created and dropped.

Here's what I have so far:

Insert = script.Parent
Upgradecost = 1

local ting = 0
function onClicked(click)
    if ting == 0 then
    check = click.Parent:FindFirstChild("Humanoid")
    if check ~= nil then
        local user = game.Players:GetPlayerFromCharacter(click.Parent)
        local stats = user:findFirstChild("leaderstats")
        if stats ~= nil then
            local cash = stats:findFirstChild("Coins")
            if cash.Value > (Upgradecost-1) then
                cash.Value = cash.Value - Upgradecost
                script.Parent.Parent.Parent.Purchase:play()
                        local h = click.Parent:findFirstChild("Humanoid")
                                if h~=nil then
                                  Coin = Instance.new("Part", game.Workspace)
                                    Coin.Shape = ("Cylinder")
                                    Coin.BrickColor = BrickColor.new("Gold")
                                    Coin.Size = Vector3.new(0.2, 2, 2)
                                    Coin.Position = Insert.Position - Vector3.new(0, 7.7, 0)
                                    Coin.Reflectance = 0.61
                                    Coin.Name = "Working"
                                    Coin.Rotation = Vector3.new(90, 0, 90)
                                    wait(1)             
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
                    end
                end
            end
        end
    end

Any ideas? No output or anything.

0
Lines 16-17 are duplicates of lines 7-8. On line 13, use cash.Value >= UpgradeCost. And most importantly, CONNECT THE FUNCTION OUTSIDE OF THE FUNCTION ITSELF. Seriously, I can't stress that enough. GoldenPhysics 474 — 8y
0
Oh my gosh! Okay I did it. But it still won't work! Tradesmark 65 — 8y
0
Put a print after each if telling what you checked. Then tell us the output GoldenPhysics 474 — 8y
0
Shouldn't your listener be outside the function... koolkid8099 705 — 8y

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

The MouseClick event of ClickDetectors takes the player who clicked as an argument. You're looking for click.Parent, and trying to get the player from that, when you already have the player.

Your connection line is also inside of your function. So, the function won't run unless the function runs. Weird.

Also, I'd try printing in different places to see what's running and what isn't, if the problem persists.

Insert = script.Parent
Upgradecost = 1

local ting = 0
function onClicked(click)
    if ting == 0 then
        check = game.Workspace:FindFirstChild(click) -- May have to use click.Name, I am unsure.
        if check ~= nil then
            local user = click
            local stats = user:findFirstChild("leaderstats")
            if stats ~= nil then
                local cash = stats:findFirstChild("Coins")
                if cash.Value > (Upgradecost-1) then
                    cash.Value = cash.Value - Upgradecost
                    script.Parent.Parent.Parent.Purchase:play()
                    Coin = Instance.new("Part", game.Workspace)
                    Coin.Shape = ("Cylinder")
                    Coin.BrickColor = BrickColor.new("Gold")
                    Coin.Size = Vector3.new(0.2, 2, 2)
                    Coin.Position = Insert.Position - Vector3.new(0, 7.7, 0)
                    Coin.Reflectance = 0.61
                    Coin.Name = "Working"
                    Coin.Rotation = Vector3.new(90, 0, 90)
                    wait(1)             
                end
            end
        end
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked) -- This should be here, so the script can run it.

Hope this helped.

EDIT: Added the fix @GoldenPhysics mentioned, didn't even notice that.

0
This worked after I changed it to "click.Name", however, although it will take money from the player, it will not create the Coin. Tradesmark 65 — 8y
0
Try it now, added in what GP mentioned. Pyrondon 2089 — 8y
0
It works perfectly! Thank you! You should seriously check out this coin pusher though, it's surprisingly legitimate. Tradesmark 65 — 8y
0
No problem, make sure you edit line 13 as @GoldenPhysics said. Pyrondon 2089 — 8y
Ad

Answer this question