Click Detector Script Help

Need some help with a problem I just can’t seem to understand… My goal is when a click detector is pressed that player is checked if they have the required amount of money. Stop If they do continue the script’s functions. If they don’t then don’t continue and stop the script without doing anything. And where and what code to put to give the money earned for winning. I can’t figure out what and where to put the code to check the players money and how to make the script respond to it. If any additional information is needed to help please let me know. Here’s the script:

‘’’

function LeverPull()

script.Parent.Pull1.Click.Transparency =1

script.Parent.Pull1.P1.Transparency = 1

script.Parent.Pull2.P1.Transparency = 0

script.Parent.Pull2.P2.Transparency = 0

wait(0.3)

script.Parent.Pull1.Click.Transparency = 0

script.Parent.Pull1.P1.Transparency = 0

script.Parent.Pull2.P1.Transparency = 1

script.Parent.Pull2.P2.Transparency = 1

end

function PlayWin()

script.Parent.Logo.Win:Play()

wait(1)

script.Parent.Logo.Win:Stop()

end

Images={

http://www.roblox.com/asset/?id=47671241”,

http://www.roblox.com/asset/?id=47671036”,

http://www.roblox.com/asset/?id=47671152”,

http://www.roblox.com/asset/?id=47671141”}

Alarm1 = script.Parent.Alarm1

Alarm2 = script.Parent.Alarm2

CoinClone = script.Parent.CoinClone

Pull1 = script.Parent.Pull1

Pull2 = script.Parent.Pull2

CD = script.Parent.Pull1.Click.ClickDetector

WinSound = script.Parent.Logo.Win

S1 = script.Parent.S1.Decal

S2 = script.Parent.S2.Decal

S3 = script.Parent.S3.Decal


function onClicked()

CD.MaxActivationDistance = 0

LeverPull()

for i = 1,40 do

wait(0.05)

S1.Texture = Images[math.random(1,#Images)]

S2.Texture = Images[math.random(1,#Images)]

S3.Texture = Images[math.random(1,#Images)]

end

for i = 1,40 do

wait(0.05)

S2.Texture = Images[math.random(1,#Images)]

S3.Texture = Images[math.random(1,#Images)]

end

for i = 1,40 do

wait(0.05)

S3.Texture = Images[math.random(1,#Images)]

end

if S1.Texture == “http://www.roblox.com/asset/?id=47671241” and S2.Texture == “http://www.roblox.com/asset/?id=47671241” and S3.Texture == “http://www.roblox.com/asset/?id=47671241” then

PlayWin()

for i = 1,10 do

wait(0.15)

Alarm1.PointLight.Enabled = true

Alarm1.BrickColor = BrickColor.new(“Really red”)

Alarm2.BrickColor = BrickColor.new(“Really red”)

wait(0.15)

Alarm1.BrickColor = BrickColor.new(“Black”)

Alarm1.PointLight.Enabled = false

Alarm2.BrickColor = BrickColor.new(“Black”)

end

for i = 1,20 do

local Coin = script.Parent.CoinClone:Clone()

Coin.Parent = script.Parent

Coin.Name = “Coin”

Coin.CanCollide = true

Coin.Anchored = false

Coin.Position = script.Parent.CoinClone.Position - Vector3.new(0,1,0)

wait(0.2)

end

wait(5)

for _, child in pairs(script.Parent:GetChildren()) do

if child.Name == ‘Coin’ then

child:Destroy()

else

end

end

CD.MaxActivationDistance = 8

else if S1.Texture == “http://www.roblox.com/asset/?id=47671036” and S2.Texture == “http://www.roblox.com/asset/?id=47671036” and S3.Texture == “http://www.roblox.com/asset/?id=47671036” then

PlayWin()

for i = 1,10 do

wait(0.15)

Alarm1.PointLight.Enabled = true

Alarm1.BrickColor = BrickColor.new(“Really red”)

Alarm2.BrickColor = BrickColor.new(“Really red”)

wait(0.15)

Alarm1.BrickColor = BrickColor.new(“Black”)

Alarm1.PointLight.Enabled = false

Alarm2.BrickColor = BrickColor.new(“Black”)

end

for i = 1,20 do

local Coin = script.Parent.CoinClone:Clone()

Coin.Parent = script.Parent

Coin.Name = “Coin”

Coin.CanCollide = true

Coin.Anchored = false

Coin.Position = script.Parent.CoinClone.Position - Vector3.new(0,1,0)

wait(0.2)

end

wait(5)

for _, child in pairs(script.Parent:GetChildren()) do

if child.Name == ‘Coin’ then

child:Destroy()

else

end

end

CD.MaxActivationDistance = 8

else if S1.Texture == “http://www.roblox.com/asset/?id=47671152” and S2.Texture == “http://www.roblox.com/asset/?id=47671152” and S3.Texture == “http://www.roblox.com/asset/?id=47671152” then

PlayWin()

for i = 1,10 do

wait(0.15)

Alarm1.PointLight.Enabled = true

Alarm1.BrickColor = BrickColor.new(“Really red”)

Alarm2.BrickColor = BrickColor.new(“Really red”)

wait(0.15)

Alarm1.BrickColor = BrickColor.new(“Black”)

Alarm1.PointLight.Enabled = false

Alarm2.BrickColor = BrickColor.new(“Black”)

end

for i = 1,20 do

local Coin = script.Parent.CoinClone:Clone()

Coin.Parent = script.Parent

Coin.Name = “Coin”

Coin.CanCollide = true

Coin.Anchored = false

Coin.Position = script.Parent.CoinClone.Position - Vector3.new(0,1,0)

wait(0.2)

end

wait(5)

for _, child in pairs(script.Parent:GetChildren()) do

if child.Name == ‘Coin’ then

child:Destroy()

else

end

end

CD.MaxActivationDistance = 8

else if S1.Texture == “http://www.roblox.com/asset/?id=47671141” and S2.Texture == “http://www.roblox.com/asset/?id=47671141” and S3.Texture == “http://www.roblox.com/asset/?id=47671141” then

PlayWin()

for i = 1,10 do

wait(0.15)

Alarm1.PointLight.Enabled = true

Alarm1.BrickColor = BrickColor.new(“Really red”)

Alarm2.BrickColor = BrickColor.new(“Really red”)

wait(0.15)

Alarm1.BrickColor = BrickColor.new(“Black”)

Alarm1.PointLight.Enabled = false

Alarm2.BrickColor = BrickColor.new(“Black”)

end

for i = 1,20 do

local Coin = script.Parent.CoinClone:Clone()

Coin.Parent = script.Parent

Coin.Name = “Coin”

Coin.CanCollide = true

Coin.Anchored = false

Coin.Position = script.Parent.CoinClone.Position - Vector3.new(0,1,0)

wait(0.2)

end

wait(5)

for _, child in pairs(script.Parent:GetChildren()) do

if child.Name == ‘Coin’ then

child:Destroy()

else

end

end

CD.MaxActivationDistance = 8

else

CD.MaxActivationDistance = 8

end

end

end

end

end

Pull1.Click.ClickDetector.MouseClick:connect(onClicked)
‘’’

1 Like

Maybe put ``` at the start and end of your script to indent it.

Indenting it isn’t the problem… I think. Why would that matter if I may ask?

Indenting is the problem. We can’t read anything you have for your code and I can’t stand to read it.
Put the backticks for the code block.

Also don’t space everything out when pasting a code in the dev forum, this can be shown as a very unprofessional thing to do. Please also indent your code correctly like so:

function OnToggle()
    -- code here
end)

@Hello42bacon Sorry this is my first post on the forums where the script is involved. How did you do it where it shows the colors and everything like in the script…

So the best thing to do is indent your code. You can do it by putting ``` at the start and at the end of your script.

1 Like

do you mean it checks the players cash and if they have enough change the parts color? If so like I said in the last post which next time you should have continued off of (Click Detector Script Help) should be like this:

script.Parent.ClickDetector.MouseClick:Connect(function(playerclicked) -- "playercliked" is the player who clicked it
    local Cash = playerclicked.leaderstats:WaitForChild("Cash")
    if Cash.Value >= 100 then -- amount of cash they must have
        Cash.Value = Cash.Value - 100 -- taking away that cash from the player
        -- code here for changing part color
    end
end)

This has to be server-sided since you are changing the players cash value btw.

Kind of. Let me try this out…

If you need more information on this clickdetector function, may i suggest checking out AlvinBlox, he is one of the best you could turn to for help because he explains everything very clearly and really helps you understand the concept. I actually learned off of him as a start to coding as a fun fact and because of him I have gotten so far with my code, Here is a clickdetector tutorial he made to help you understand it more:

This worked!
thx
One more question. This Function:

function PlayWin()

script.Parent.Logo.Win:Play()

wait(1)

script.Parent.Logo.Win:Stop()

end

this function is played later in the previous function I just fixed. But how would this one read the player and give the reward?

you can actually simply play the function after it does the transaction in the script like so:

script.Parent.ClickDetector.MouseClick:Connect(function(playerclicked) -- "playercliked" is the player who clicked it
    local Cash = playerclicked.leaderstats:WaitForChild("Cash")
    if Cash.Value >= 100 then -- amount of cash they must have
        Cash.Value = Cash.Value - 100 -- taking away that cash from the player
        PlayWin()
        -- code here for changing part color
    end
end)

If you’re binding PlayWin to a MouseClick event you can get the player as an argument.

function PlayWin(player)
    print(player.Name.." clicked the button.")
    script.Parent.Logo.Win:Play()
    wait(1)
    script.Parent.Logo.Win:Stop()
    end

edit: @Hello42bacon explains it better.

I have that already. Im trying to make the function “PlayWin()” read the player who clicked that same click detector and give them a reward…

1 Like

Well you can add the player parameter to the function call and give them their reward inside the PlayWin() function like this:

-- code above
PlayWin(playerclicked)
--code

Function:

function PlayWin(player) -- parameter recieved
    -- code here
end)

Here is an article on functions, AlvinBlox should have a tutorial on it too: Functions | Documentation - Roblox Creator Hub

1 Like

Never thought of that let me try it

Works. Thx for the help. Ima send a friend request maybe you can help me in the future if I need it. I will give you credit on my game. Thank you again!
also I will check those vids out…