Mouse function problem

So I am pretty new to scripting and I tried to do a script for the first time without any help from a reference. I tried to do it so that if you click a UI it gives you a “coin”. However I tried out this script in the button but it did not change to 1.

2 Likes

Everything in StarterGui it’s automatically cloned to the PlayerGui when the game runs. Instead you can use Player.PlayerGui or use script.Parent to localize the button.

2 Likes

As @Sarchyx stated, use script.Parent, as it is the easiest. So the code should be.

local NumberofCoins = script.Parent.NumberOfCoins
script.Parent.MouseButton1Click:Connect(function()
NumberofCoins.Text = 1
end)
1 Like

I put it under the ScreenGUI and did what you said but it still does not work

1 Like
local NumberofCoins = script.Parent.NumberOfCoins
print("This is online and working")

script.Parent.MouseButton1Down:Connect(function()
    print("Event fired")
    NumberofCoins.Text = "1"
end)

Can you check your Output for any odd instances?

why not use parenting like @Amazin_Builds, @JackscarIitt and @Sarchyx said? Try this @BellingerMyBro

local N.Text = 0
local N = script.parent.parent.NumberofCoins
script.Parent.MouseButton1Click:Connect(function()
N.Text = N.Text +1
end)

and where exactly did you put your script? I cant see it in the Explorer

no because StarterGUI is not in the workspace? So how exactly would that work? it doesnt

1 Like

Oh yeah @BellingerMyBro sorry bout that but the script should work

I tried the script but it didnt work

Where’s the place of script? <-----------

Do you want When player click at the button. he gets a coin! and text will be changing after that???

Can you example the thing clearly… <--------------

  1. I have my script under the screen GUI but not under the text button

  2. Yes

  3. When I click the button the text does not change to ‘1’ which is what I want it to do.

You mean under StarterPack?
I don’t know actually.
if you want a gui button. when player click it. he gets a coin.
or player have to tap at screen.
Well try this code.
If didn’t work
tell me!

local NumberOfCoins = game.StarterGui.ScreenGui:WaitForChild("NumberOfCoins") -- Wait for the thing to be loaded.
local working = true -- Things working fine.
warn(working) -- will show message at output. it's working.

script.Parent.MouseButton1Click:Connect(function() -- When player clicks.
          if NumberOfCoins.Text == "0" then -- Checking if the text is still at 0.
NumberOfCoins.Text = "1" -- Making it to 1.
else -- returning
NumberOfCoins.Text = "0" -- If it's still 0. then it will try to change it!

end 
end)
-- ends
  1. so u put the script under the screengui so now define it not by using startergui but do script.parent.CoinGiver

  2. i think TextButtons use .Activated instead of .mousebutton1click

I tried this script out but it isn’t changing the text. The output however says true.

I also changed the fifth line to game.StarterGui because the parent of the script was ScreenGui but i don’t think that would affect it.

Ok tell me.
1- Do you want player to tap at screen to get coin?
2- or a button gui to get the coin.

Did you place the script at starterpack.

its a button gui and yes i placed in the starterpack

1- Add ScreenGui.
2- Add a textlabel.
3- Add local script at it.
Here’s the script

while wait() do
local plr = game.Players.LocalPlayer
script.Parent.Text = “Coins:”…plr:WaitForChild(“leaderstats”):FindFirstChild(“Coins”).Value – Checks the coins at leaderstats value. to change the text as the same value.
end

–Change coins to your value name. don’t forget that!
–Did you create leaderstats for that. [This is a important thing]

Wait Im a bit confused? did you want me to put the text label in a seperate screenGui or the same one?

here ill send a screenshot

1- Did you create leaderstats first for coins.

2- Do you want to make simulator game??

Yeah Ill create a leaderstat and Im just doing this to help myself out for the future

So this is what I have right now

Sorry if hard to see –