This is the second time I have to post this, sorry if I shouldn’t do that but I do not know what to do. My scripts are legitimate but they don’t seem to be showing in game.
First problem:
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
wait(1)
game.Workspace.Values.Warning.Value = true
end
end)
I made this code so that when a player walks through an invisible part, the collision activates a bool value which sets off a series of events necessary to the experience. Through play-testing, I see that the “warning” value, as its called, is set to true in the properties menu but will not trigger the events attached to it that are in a separate script. It only works when the value is set as true before play-testing the game.
What am I doing wrong? Why will the value triggered in-game not do what it’s supposed to?
Second problem (shortened to highlight area of concern):
local tv = script.Parent
local eas = tv.EAS
local ssss = tv.Static
local screen = tv.SurfaceGui
local warning = game.Workspace.Values.Warning
local outage = game.Workspace.Values.Outage
if warning.Value == true then -- events supposed to happen in problem one
screen.Enabled = true
ssss.Volume = 0
eas:Play()
wait(33)
warning.Value = false
outage.Value = true
end
if outage.Value == true then
wait(1)
game.Workspace.STORMSOUNDS.ThunderRumble:Play()
wait(3)
game.StarterGui.ScreenGui.TextBox.Visible = true
game.StarterGui.ScreenGui.TextBox.Text = "[Text]"
wait (5)
game.StarterGui.ScreenGui.TextBox.Visible = false
end
This code is supposed to simulate a power outage so that the experience can progress. For the most part, it works and reflects well in game. However, in the “if outage” situation, only half of the events in the string happen until after wait(1) where none of what is below it happens. What’s the deal with that?
Again, sorry that this is such a dense issue but I am a beginning developer whose knowledge is completely based off YouTube tutorials and DevForum direction. I’m at a loss here. Please ask questions if you need more understanding or context.
First problem: if warning.Value == true then only runs once when the script is run. To listen for the value changed you need to use a signal: warning.Changed:Connect(NameOfYourFunction) this will run your function everytime the value has been changed.
local function WarningUpdated(NewValue)
if NewValue == true then
screen.Enabled = true
ssss.Volume = 0
eas:Play()
wait(33)
warning.Value = false
outage.Value = true
end
end
warning.Changed:Connect(WarningUpdated)
Second problem:
You don’t mention the type of script your using so depending on wether it’s a server script or local script the answer will be different.
The problem is your using StarterGui but you should be using PlayerGui. But to use PlayerGui you need the Player instance of the player who’s gui you want to change.
If it’s a local script then it’s easy enough as you could just use the following code:
local playerGui=game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
playerGui.ScreenGui.TextBox.Visible = true
If it’s a server script then you will probably want to use a remote event to fire allplayers to update their guis. This is slighly more complicated but you can read all about how to set them up and use them here:
Your first solution worked in solving the first problem, but also seemed to break the code for the outage. Can you tell me why that happened and how to fix it for myself? Not asking for you to modify the script for me or anything because I need to learn this stuff.
Also, I was using regular scripts thorughout the game (which may have been a mistake) so I’m going to see about remote events. Should I only use local scripts for single-player experiences like this one?
Regular scripts make it easier to keep your game data safe. I would stay with them. Local scripts can be easily stolen. In what way did it break the outage script?
You would need to setup a similar signal for when the outage value changes too:
local function WarningUpdated(NewValue)
if NewValue == true then
screen.Enabled = true
ssss.Volume = 0
eas:Play()
wait(33)
warning.Value = false
outage.Value = true
end
end
local function OutageUpdated(NewValue)
if NewValue == true then
wait(1)
game.Workspace.STORMSOUNDS.ThunderRumble:Play()
wait(3)
game.StarterGui.ScreenGui.TextBox.Visible = true -- These will still need to be altered
game.StarterGui.ScreenGui.TextBox.Text = "[Text]" --
wait (5)
game.StarterGui.ScreenGui.TextBox.Visible = false
end
end
warning.Changed:Connect(WarningUpdated)
outage.Changed:Connect(OutageUpdated)
It actually works but since you are referencing game.StarterGui.ScreenGui.TextBox.Visible instead of referencing the PlayerGui, it won’t actually render, so my solution would be:
Fire event from server to client
If outage is true, or warning is true, then set the PlayerGui’s ScreenGui Enabled Property to true
This is a regular script. Since @ZombieCrunchUK’s solution, all of the things supposed to happen when the “outage” value is set to true doesn’t occur. I’ve tried many ways to recover it but all void.
Since you’ve said you are a beginner it may help to simply show you a full example as it may be a little easier for you to see/understand. I’ve also included the remote event part. ServerScript:
local tv = script.Parent
local eas = tv.EAS
local ssss = tv.Static
local screen = tv.SurfaceGui
local warning = workspace.Values.Warning
local outage = workspace.Values.Outage
local remoteEvent=game:GetService("ReplicatedStorage"):WaitForChild("myRemoteEvent")
local function WarningUpdated(NewValue)
if NewValue == true then
print("warning Value Updated to true") -- Debug message, shows in output, delete once everything is working
screen.Enabled = true
ssss.Volume = 0
eas:Play()
task.wait(33)
warning.Value = false
outage.Value = true
end
end
local function OutageUpdated(NewValue)
if NewValue == true then
print("outage Value Updated to true") -- Debug message, shows in output, delete once everything is working
task.wait(1)
workspace.STORMSOUNDS.ThunderRumble:Play()
task.wait(3)
remoteEvent:FireAllClients()
end
end
warning.Changed:Connect(WarningUpdated)
outage.Changed:Connect(OutageUpdated)
LocalScript (Created in your ScreenGui [StarterGui->ScreenGui]
local remoteEvent=game:GetService("ReplicatedStorage"):WaitForChild("myRemoteEvent")
local myScreenGui=script.Parent
local myTextBox=myScreenGui:WaitForChild("TextBox")
local function UpdateGui()
print("remoteEvent Triggered") -- Debug message, shows in output, delete once everything is working
myTextBox.Visible = true
myTextBox.Text = "[Text]" --
task.wait(5)
myTextBox.Visible = false
end
remoteEvent.OnClientEvent:Connect(UpdateGui)
Create a RemoteEvent in ReplicatedStorage and rename it to myRemoteEvent
I tested this in my studio and it works as expected with the following output in the output window:
warning Value Updated to true - Server - Script:10
outage Value Updated to true - Server - Script:22
remoteEvent Triggered - Client - LocalScript:7
Sorry for the day-late response.
Yeah, this made everything work at last! But guess what. I was confused firstly, because even with your fixes the sound and GUI continued to not function. It wasn’t until I had to move both of them up the string to be the first things that happen when outage is triggered that they decided to show. I mean… what?
Either way, I’m glad to have this significant roadblock figured out. Thanks for your help, I really appreciate this. I’ll remember this for evermore complicated coding I have to do in the future.
P.S. I see that a lot of people put print in their code to show that its functioning properly. Should I start doing this too?
Using print can be very useful for debugging your code, it just gives you visual cues as to what part of your code the game is currently reading etc. You can see when certain functions have been called (or not called when no print shows up) so yes, I would recommend adding temporary prints in your code while figuring out what works/doesn’t work. Once everything is working and your happy with certain sections/functions you can just remove the print statements.