#I need help with a spectate system.

1 messages · Page 1 of 1 (latest)

crystal oxide
#

I am making a spectate system and I want the Spectate Gui to get removed when I am not in a round.
I have a bool value that works which checks to see if I am in a round or not and it is false when I am not.
The bool value is in Players."My Username" but when I try to disable it it doesnt do anything.

hidden hare
#

Also if the script is a local script that could be another issue

silk island
crystal oxide
#

It wasnt a local script and here is the script local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
local isPlaying = Instance.new("BoolValue")
isPlaying.Name = "isPlaying"
isPlaying.Value = false
isPlaying.Parent = player

local SpectateGui = game.StarterGui:WaitForChild("SpectateGui")

if isPlaying == false then
    SpectateGui.Enabled = true
else
    SpectateGui.Enabled = false
end

end)

loud ingot
#

It should be isPlaying.Value == false

#

Currently, you are checking if the instance itself (BooleanValue) is false, where you should be checking its Value attribute

crystal oxide
#

I am an idiot how did i not see that

#

It still doesnt work though

loud ingot
#

It's probably cuz of the variable's name too lol, we've all been there

#

To make it cleaner, don't do == false. Instead, do if not isPlaying.Value then

loud ingot
#

Check if it really is enabled and that the GUI elements in it are visible and whatnot

crystal oxide
#

So,

local SpectateGui = game.StarterGui:WaitForChild("SpectateGui")

if isPlaying.Value then
    SpectateGui.Enabled = true
else
    SpectateGui.Enabled = false
end

end)

loud ingot
#

I'm assuming isPlaying is false when not in a round, right?

crystal oxide
#

Yes

loud ingot
#

Then it should be if not isPlaying.Value then

#

The not negates the boolean, so true becomes false and vice versa

crystal oxide
#

Or swap the False and true around?

loud ingot
#

Yup that too

crystal oxide
#

Still doesnt work

#

Should I send a video?

loud ingot
#

Sure

loud ingot
loud ingot
crystal oxide
#

OK

#

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
local isPlaying = Instance.new("BoolValue")
isPlaying.Name = "isPlaying"
isPlaying.Value = false
isPlaying.Parent = player

local SpectateGui = game.StarterGui:WaitForChild("SpectateGui")

if isPlaying.Value then
    SpectateGui.Enabled = false
else
    SpectateGui.Enabled = true
end

end)

loud ingot
#

So it works, it appears when the player first joined since they're not in a round. Now you want it to disappear when a round starts?

crystal oxide
#

I turned it on when I joined and i want it off when the round starts

snow vineBOT
#

studio** You are now Level 4! **studio

crystal oxide
#

So people cant spectate others and see what they are doing

loud ingot
#

To do this autonomously you could add a :Changed listener to your isPlaying BooleanValue, so that whenever you update the value of you IsPlaying instance, it automatically changes it as well

crystal oxide
#

Could you help me I dont know how as i am learning lua

loud ingot
#

Do something like this:

IsPlaying.Changed:Connect(function(newValue: boolean)
    if isPlaying.Value then... -- etcetera
end)

To break it down...

IsPlaying.Changed is the event.

:Connect(someFunction) means that, whenever the value of IsPlaying changes, we run someFunction

snow vineBOT
#

studio** You are now Level 4! **studio

loud ingot
#

Now, whenever you do IsPlaying.Value = false or IsPlaying.Value = true, it'll automatically run our someFunction inside the :Connect(someFunction) connection

crystal oxide
#

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
local isPlaying = Instance.new("BoolValue")
isPlaying.Name = "isPlaying"
isPlaying.Value = false
isPlaying.Parent = player
local SpectateGui = game.StarterGui:WaitForChild("SpectateGui")

isPlaying.Changed:Connect(function(newValue: boolean)
    if isPlaying.Value then
        SpectateGui.Enabled = false
    else
        SpectateGui.Enabled = true
    end)
#

?

loud ingot
#

Exactly 👍

Though, for the first bit, it won't run since you've already changed the value to false before you've set up the connection lol. Put it in a local function instead and use that

local function onIsPlayingChanged(isPlaying: boolean)
    if isPlaying then
        SpectateGui.Enabled = false
    else
        SpectateGui.Enabled = true
    end)
end

isPlaying.Changed:Connect(onIsPlayingChanged) -- This tells roblox that, from this moment on, run onIsPlayingChanged when isPlaying.Value is updated

onIsPlayingChanged(false) -- We run it at the start too
crystal oxide
#

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
local isPlaying = Instance.new("BoolValue")
isPlaying.Name = "isPlaying"
isPlaying.Value = false
isPlaying.Parent = player
local SpectateGui = game.StarterGui:WaitForChild("SpectateGui")

#

local function onIsPlayingChanged(isPlaying: boolean)
if isPlaying then
SpectateGui.Enabled = false
else
SpectateGui.Enabled = true
end)
end

isPlaying.Changed:Connect(onIsPlayingChanged) -- This tells roblox that, from this moment on, run onIsPlayingChanged when isPlaying.Value is updated

onIsPlayingChanged(false) -- We run it at the start too

#

?

loud ingot
#

yup

crystal oxide
#

Why is there an error on "false"

#

?

loud ingot
#

Put them all inside .PlayerAdded.

crystal oxide
#

How it still gives errors

loud ingot
#

You're missing a ) on your last end

crystal oxide
#

?

loud ingot
#

You're also missing and end for your local function

#

You can figure it out, it's all simply syntax errors now. Understand each part of the code and organize it

crystal oxide
#

The Gui still stays on

loud ingot
#

Yes, you still have errors on your code

#

Can you screenshot it again now

crystal oxide
#

ok

#

btw sorry for asking all these questions

loud ingot
#

Looks good logically, but could do better in indenting and general beautifying.

Your problem now is you're getting the SpectateGui from the StarterGui, when you should get it from the player's PlayerGui.

Roblox simply clones what's in StarterGui into the player's PlayerGui, so with that in mind, in your code, it's like you're editing the blueprint rather than what the player has.

On line 8, instead of game.StarterGui, do player.PlayerGui

crystal oxide
#

That worked.

#

Also there is an issue that doesnt break anything but is annoying could you help with that quickly(Still to do with spectate system)]

loud ingot
#

Nice. There's a much more optimized way of doing this though, which involves using localscripts to handle the toggling of the SpectateGui. Currently you're letting the server deal with the toggling of each player's gui, so it's not that efficient atm

loud ingot
crystal oxide
#

Ok

hidden hare
silk island