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 5 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?

ManyPlayerValue = game.StarterGui.ScreenGui.ManyPlayerValue
VL = ManyPlayerValue.Value
GameStarted = game.StarterGui.ScreenGui.GameStarted
VLGS = GameStarted.Value

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

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

game.Players.ChildAdded:connect(onPlayerEntered)
game.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 — 5y

3 answers

Log in to vote
2
Answered by 5 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


local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local VL = plr.PlayerGui.ScreenGui.ManyPlayerValue
local GameStarted = plr.PlayerGui.ScreenGui.GameStarted

local function updateVL ()
    VL = #Players:GetPlayers()
end

Players.PlayerAdded:Connect(function(plr)
    updateVL()
end)

Players.PlayerRemoving:Connect(function(plr)
    updateVL()
end

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 5 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

ManyPlayerValue = game.StarterGui.ScreenGui.ManyPlayerValue
VL = 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)

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 — 5y
Log in to vote
0
Answered by 5 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