Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Convert onClicked button to Onclicked GUI?

So ive got this script that atm runs on an OnClicked Brick so when u click the brick it starts my dice rolling game. But im wanting to convert to it to a GUI system instead of through a brick, but im lost on how to do this, here is what ive got

The part im hung up on (i think)

part = clicked.Character.Head
local h = part.Parent:findFirstChild("Humanoid")

This script is a product of help from other community members when i was using it for something else, and this part was added to make it work, but this is for a "Part" within workspace, idk how to change it for a GUI within startGui, idk if this matters but figured i would throw it in.

function onTouched(clicked)
    part = clicked.Character.Head
    local h = part.Parent:findFirstChild("Humanoid")
    if (h~=nil) then
            local thisplr = game.Players:findFirstChild(h.Parent.Name)
            if (thisplr~=nil) then
                local stats = thisplr:findFirstChild("leaderstats")
                if (stats~=nil) then
                    local score = stats:findFirstChild("Gold")
                    if (score~=nil) then
                        local Dice = game.Players.Player.PlayerGui.Roller.TextLabel
                        local Rolls = math.random(100)
                        for _ = 1, 1 do

                            print(Rolls)
                            if Rolls >= 55 then
                                score.Value = score.Value + 1
                                Dice.Visible = true
                                Dice.Text = ("You rolled a ") .. Rolls .. ("!")
                                wait(2)
                                Dice.Visible = false
                            else
                                Dice.Visible = true
                                Dice.Text = ("You rolled a ") .. Rolls .. ("...") 
                                wait(2)
                                Dice.Visible = false
                            end
                            wait(.1)
                        end

                    end
                end
            end
        end
    end

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

Any help steering me in the right direction is appreciated :)

(UPDATE) Its a local script now This is what i have right now

bin = script.Parent
function Click()
	local h = click.Parent:findFirstChild("Humanoid") --IDK how to change this part because "click" is no longer in use
	if (h~=nil) then
			local thisplr = game.Players:findFirstChild(h.Parent.Name)
			if (thisplr~=nil) then
				local stats = thisplr:findFirstChild("leaderstats")
				if (stats~=nil) then
					local score = stats:findFirstChild("Gold")
					if (score~=nil) then
						local Dice = game.Players.LocalPlayer.PlayerGui.Roller.TextLabel
						local Rolls = math.random(100)
						for _ = 1, 1 do
							
							print(Rolls)
							if Rolls >= 55 then
								score.Value = score.Value + 1
								Dice.Visible = true
								Dice.Text = ("You rolled a ") .. Rolls .. ("!")
								wait(2)
								Dice.Visible = false
							else
								Dice.Visible = true
								Dice.Text = ("You rolled a ") .. Rolls .. ("...") 
								wait(2)
								Dice.Visible = false
							end
							wait(.1)
						end

					end
				end
			end
		end
	end


bin.MouseButton1Click:connect(Click)

Line 3 is my problem

1 answer

The TextButton object has several events like MouseButton1Down and MouseButton1Click which can be used. You can find more here. Also, recently I noticed that GUI related events only fire on LocalScripts, so keep that in mind.

Another thing I noticed is that you're using game.Players.Player. This will only work in Studio, not online. If you're using a LocalScript, change this to game.Players.LocalPlayer.

1 comment

0

i updated my question with my new code and my problem

This discussion is preserved as it was. New answers, comments, and votes are closed.