Need help with adding intValue on GUI button clicked

Hello, I’m Techyfied. I’m making a question based game where - if you press yes button, it will add a value to a intValue (ProbabilityMetre). If you press no button - it will not. But the problem is, even if i press the yes button - The value never changes.

GUIs Location:
image

Yes button script:

Button = script.Parent

CurrentQuestion = script.Parent.Parent
NextQuestion = script.Parent.Parent.Parent.QuestionThree

Probability = script.Parent.Parent.Parent.ProbabilityMetre

function onClick() 	
	Probability.Value = Probability.Value + 1
	print("Added a value to your probability metre!")
	CurrentQuestion.Visible = false
	wait(1)
	NextQuestion.Visible = true
end 

Button.MouseButton1Click:connect(onClick) 

Value print script inside “Starter” script:

pv = script.Parent.ProbabilityMetre.Value
print("Done! Value is".. pv.. "!!")

I have tested it multiple time, the output is always “Done! Value is0!!”


I’m new to scripting and I am not sure if I could not do a easy task and wasted your time. Thank you for using your valuable time on reading this :smiley:

One mistake I can already see from your picture is that you’re using regular script. For GUIs, refrain from using server scripts and use local scripts instead, because the GUI changes are specific to each player.

4 Likes

But its max player is 1. Will it still affect?

Yes it will affect, server scripts are not supposed to run on GUIs. But if it does work, not recommended.

Also make sure you’re checking the GUI which is not the one under StarterGui. Because when player spawns, that GUI will be cloned to each players (under PlayerGui, specifically), so make sure you’re looking at the right spot.

Here’s a picture:
https://gyazo.com/0224049ac6b3cce25d270ddd515783e0

So what should I do? Turn them into local script? Or should I make it appear from playergui and the value will be at workspace?

If you don’t want too much work, then I think you’re fine without changing it to local script since it’s a 1 player game (this may or may not pose problem in the future).

As for the value that doesn’t change, you have to playtest in Studio in order for the player instance to come up and lets you see the cloned PlayerGui, and in it you can see if the ProbabilityMetre changed.

You are right they are not supposed to but they actually do run in descendants of PlayerGui. However the only reason server scripts don’t run with GUI most of the time is because people have Filtering Enabled on, which replicates Starter Gui’s components to Player Gui , including scripts.

The Server is not aware of the existance of Server Scripts that are replicated to Starter Gui, this results in Server scripts not working with Gui.

1 Like

FilteringEnabled is no longer in play here. I am aware that server scripts do run within PlayerGui.

1 Like

Thanks, i will try it and let you know the result when i get on pc.

I turned them into local script, but didnt work

You would want to use a RemoteEvent and connect a Script to the OnServerEvent on that RemoteEvent

1 Like

Well, I am really knew to scripting specially functions. I would be really helpful if you tell me where to put the remote event, what to add in which scripts and what should I do more. Thank you.

If you want me to help you further, reach me out on Discord: slothfulGuy#5064.

Added (30 charrrrrrrrrrrrrrrr)

@slothfulGuy If the problem has not been resolved yet, try to continue here so other readers can benefit from the content.


@Techyfied

Try using a Local Script instead of a Server script and fire an event containing the value to increment Probability on the Server.
You should have put the value ProbabilityMetre somewhere else, preferably in a folder parented to the player, but this works too.

Local Script for demonstration purposes :

  local event = game:GetService("ReplicatedStorage").IncrementEvent
  local Amount = 1   

  local button = script.Parent 
  clicked = false
 
 
  button.MouseButton1Click:Connect(function() 
  
         if not clicked then
                   clicked = true      
                   event:FireServer(Amount)
              wait(3)  --// cooldown
         clicked = false
      end
  end)

A server script, preferably in Server Script Service :
local event = game:GetService("ReplicatedStorage").IncrementEvent
  
      event.OnServerEvent:Connect(function(player, value)
  
            local PlayerGui = player:FindFirstChild("PlayerGui")
            local prob = PlayerGui.MainScreen.ProbabilityMetre
 
       prob.Value = prob.Value + value
   
       print("Probability is now"..tostring(prob.Value)) 
   end)
1 Like

Thanks, I will try it and let you know the result!

You should try incrementing the value on the Server so the value can be saved using Datastores.

Thanks! Turning all scripts into local script worked for me!