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

NumberValue does not change. How to do it?

Asked by 6 years ago

Why when I registered in the script so that the value of NumberValue changes when the player connects, in TextLabel Everything is normally written and works as it is written in the script, but when I want to somehow interact with NumberValue in another script, its value is 0. Why?

01ManyPlayerValue = game.StarterGui.ScreenGui.ManyPlayerValue
02VL = ManyPlayerValue.Value
03GameStarted = game.StarterGui.ScreenGui.GameStarted
04VLGS = GameStarted.Value
05 
06function onPlayerEntered(plyr)
07    VL = VL + 1
08    game.StarterGui.ScreenGui.Frame.ManyPlayers.Text = VL .. "/4"
09end
10 
11function onPlayerExited(plyr)
12    VL = VL - 1
13    game.StarterGui.ScreenGui.Frame.ManyPlayers.Text = VL .. "/4"
14end
15 
16game.Players.ChildAdded:connect(onPlayerEntered)
17game.Players.ChildRemoved:connect(onPlayerExited)
0
u entered value variable 2 times remove the VL and change name of ManyPlayerValue variable to: "VL". number 2. change in onPlayerEntered the game.StarterGui.ScreenGui.Frame.ManyPlayers = VL.Value .. "/4" instead of VL without value boshjio15854 15 — 6y

3 answers

Log in to vote
2
Answered by 6 years ago

Problems


A couple of things here

  • Multiple variables of yours are values, instead of references, meaning that when the values of those objects are changed, your variables aren't updated.

  • You are updating the guis in the StarterGui, not the PlayerGui as you should be, All objects within the StarterGui are cloned into the PlayerGui locally when a player joins a game

  • The PlayerAdded and PlayerRemoving events are recommended as opposed to ChildAdded and ChildRemoved, as they ensure that the object added is a player object.


Solution


01local Players = game:GetService("Players")
02local plr = Players.LocalPlayer
03local VL = plr.PlayerGui.ScreenGui.ManyPlayerValue
04local GameStarted = plr.PlayerGui.ScreenGui.GameStarted
05 
06local function updateVL ()
07    VL = #Players:GetPlayers()
08end
09 
10Players.PlayerAdded:Connect(function(plr)
11    updateVL()
12end)
13 
14Players.PlayerRemoving:Connect(function(plr)
15    updateVL()
16end

Other things


  • PlayerAdded / game.Players.ChildAdded doesn't fire in local scripts for the local player also those scripts are cloned into the player after the event fires

  • You can't access anything in the PlayerGui from the server as it is cloned locally

  • There is a GetPlayers function of the Players Service that returns an array of all players currently in game

Hopefully this helped!

Ad
Log in to vote
0
Answered by 6 years ago

when you save ManyPlayerValue.Value (or GameStarted.Value) to the variable, its always a copy of it everywhere else, try getting the value when needed

01ManyPlayerValue = game.StarterGui.ScreenGui.ManyPlayerValue
02VL = ManyPlayerValue
03GameStarted = game.StarterGui.ScreenGui.GameStarted
04VLGS = GameStarted.Value
05 
06function onPlayerEntered(plyr)
07    VL.Value = VL.Value + 1
08    game.StarterGui.ScreenGui.Frame.ManyPlayers.Text = VL.Value .. "/4"
09end
10 
11function onPlayerExited(plyr)
12    VL.Value = VL.Value - 1
13    game.StarterGui.ScreenGui.Frame.ManyPlayers.Text = VL.Value .. "/4"
14end
15 
16game.Players.ChildAdded:connect(onPlayerEntered)
17game.Players.ChildRemoved:connect(onPlayerExited)

another possibility is that you arent getting the actual value properly, when getting an instance it must start with game., workspace., or script. you cant just reference it's name and expect roblox to automatically know what that is

0
that still wouldn't work as you still are  using StarterGui theking48989987 2147 — 6y
Log in to vote
0
Answered by 6 years ago

u entered value variable 2 times remove the VL and change name of ManyPlayerValue variable to: "VL". number 2. change in onPlayerEntered the game.StarterGui.ScreenGui.Frame.ManyPlayers = VL.Value .. "/4" instead of VL without value~~~~~~~~~~~~~~~~~ VL = game.StarterGui.ScreenGui.ManyPlayerValue GameStarted = game.StarterGui.ScreenGui.GameStarted VLGS = GameStarted.Value

function onPlayerEntered(plyr) VL.Value = VL.Value + 1 game.StarterGui.ScreenGui.Frame.ManyPlayers.Text = VL.Value .. "/4" end

function onPlayerExited(plyr) VL.Value = VL.Value - 1 game.StarterGui.ScreenGui.Frame.ManyPlayers.Text = VL.Value .. "/4" end

game.Players.ChildAdded:connect(onPlayerEntered) game.Players.ChildRemoved:connect(onPlayerExited) ~~~~~~~~~~~~~~~~~

Answer this question