I’m trying to get a script to run when the player joins, which will change a value under a player’s Gui and as a result, set another script in motion. The changing of the value works with the first script, but for some reason the script that needs to detect that change will not do so??? (This has worked for all other scripts in my game so far.) Could be a simple issue but I have tried for a few hours to find the source of the issue and its getting tiring. I do not know if it isn’t registering the fact that the value changed because it happened too quickly after the player joined or if it is an error in the code.
Value - InMenu (Originally set to false)
Script (Changing the value to true upon the player joining the game, is under ServerScriptService) -
game.Players.PlayerAdded:Connect(function(plr)
local val = true
local val1 = true
while val do
if plr.Character ~= nil then
plr.Character:WaitForChild("HumanoidRootPart").Anchored = true
val = false
end
wait()
end
plr.PlayerGui:WaitForChild("Menu").TitleScreen.InMenu.Value = true
print("Done") --This part does print in the output as intended
end)
LocalScript (Detecting the changed value, is under StarterPlayerScripts) - TitleScreen
local plr = game.Players.LocalPlayer
--TweenService
local TweenService = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(70)
plr.PlayerGui:WaitForChild("Menu").TitleScreen.InMenu.Changed:Connect(function()
print("Value Change Detected") -- Does not run
end)
Please help
If it is a simple or unsolvable error, any alternative suggestions???
I’ve tested your script and it’s run fine for me. There’s a few issues I imagine you might be running into though:
If you’re testing this in studio, it’s a known bug that the PlayerAdded event does not always fire as intended. This can be fixed by changing your ServerScriptService script to look like this:
function PlayerAdded(plr)
local val = true
local val1 = true
while val do
if plr.Character ~= nil then
plr.Character:WaitForChild("HumanoidRootPart").Anchored = true
val = false
end
wait()
end
plr.PlayerGui:WaitForChild("Menu").TitleScreen.InMenu.Value = true
print("Done") --This part does print in the output as intended
end
game.Players.PlayerAdded:Connect(PlayerAdded)
for _, plr in game.Players:GetPlayers() do
PlayerAdded(plr)
end
We can loop through players to make sure we can change the value for anyone that PlayerAdded has missed.
On the other end of things, it’s possible that the value has already been changed by the time you begin listening for the change. You can do something like this to fix that problem:
local plr = game.Players.LocalPlayer
--TweenService
local TweenService = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(70)
local menuValue = plr.PlayerGui:WaitForChild("Menu").TitleScreen.InMenu
if menuValue.Value == true then
print("Too Late For Value Change")
end
menuValue.Changed:Connect(function()
print("Value Change Detected")
end)