i am developing a game and my script wont run in serverscript service(longscript) first script(in startergui inside a textbox)
01 | timer = script.Parent |
02 | val = game.ReplicatedStorage.Timer |
03 | intro = game.ReplicatedStorage.Intro |
04 | t = game.ReplicatedStorage.T |
05 |
06 | while true do |
07 | if intro.Value = = 1 then -- part that was referenced in script below |
08 | timer.Visible = true -- timer wont go visible |
09 | timer.Text = "0:" ..val.Value |
10 | val.Value = val.Value - 1 -- timer wont go down a second |
11 | end |
12 | if timer.Text = = "0:0" then -- this part works fine |
13 | local it = game:WaitForChild( "ReplicatedStorage" ).It |
14 | it:FireServer() |
15 | timer.Visible = false |
16 | intro.Value = 0 |
17 | timer.Text = "0.0" |
18 | end |
19 | end |
this is the script with the problem(Below)
01 | local gamie = game.ReplicatedStorage.Game |
02 | local intro = game.ReplicatedStorage.Intro |
03 | local timer = game.ReplicatedStorage.Timer |
04 | lava = game.ReplicatedStorage.Lava |
05 | local rs = game:GetService( 'ReplicatedStorage' ).Restart |
06 | local t = game:GetService( "ReplicatedStorage" ) |
07 |
08 | rs.Event:Connect( function (touch) |
09 | timer.Value = 20 -- this is supposed to change the value of the timer to 20 to count down but that wont change either...??? |
10 | intro.Value = 1 --Part that is supposed to trigger timer(as shown in script above) |
11 | local clone = game.ReplicatedStorage.Lava 2 :Clone() |
12 | clone.Parent = game.Workspace |
13 | if lava.Parent = = game.ReplicatedStorage then |
14 | lava.Parent = game.Workspace |
15 | end |
Usually, you would want to avoid any loop with the wait() function or not. It is often best to use :GetPropertyChangedSignal(property) to fire the function rather than always check for a value. This will reduce the lag significantly as it only functions when the value is changed.
01 | timer = script.Parent |
02 | val = game.ReplicatedStorage.Timer |
03 | intro = game.ReplicatedStorage.Intro |
04 | t = game.ReplicatedStorage.T |
05 |
06 | intro:GetPropertyChangedSignal( "Value" ):Connect( function () |
07 | if intro.Value = = 1 then -- part that was referenced in script below |
08 | timer.Visible = true -- timer wont go visible |
09 | timer.Text = "0:" ..val.Value |
10 | val.Value = val.Value - 1 -- timer wont go down a second |
11 | end |
12 | if timer.Text = = "0:0" then -- this part works fine |
13 | local it = game:WaitForChild( "ReplicatedStorage" ).It |
14 | it:FireServer() |
15 | timer.Visible = false |
16 | intro.Value = 0 |
17 | timer.Text = "0.0" |
18 | end |
19 | end ) |
Additionally, you should also use the tonumber() function just so the timer doesn’t break if text is entered into the text box. As for the final thing I noticed, you should probably put some data into the :FireServer() event so that the server can read the values of the timer and other data you sent.
Edit: I can’t fully help you because you didn’t paste the entirety of the scripts
I don't know the full context of your situation or what your trying to do but i hope this helps. I would insert a NumberValue in your ScreenGui or really where ever you want just change the script according to your game and set the text to the NumberValue. I would also use a for loop instead of a while loop for your timer that you can call with a remote event when you want to use this. Again not sure if this is what you were looking for but I hope this can help in some way.
01 | game.ReplicatedStorage. "Your remote event" .OnClientEvent:Connect( function () --Instead of waiting for intro.Value == 1 call the timer using a remote event when you set the intro.Value to 1 |
02 | local seconds = script.Parent.Parent.Parent.Time --This is the NumberValue within the ScreenGUI |
03 | local timer = script.Parent --The text label showing the time |
04 | seconds.Value = 20 |
05 | script.Parent.Text = seconds.Value --This sets the Text to whatever the NumberValue is so changing it will change the text |
06 |
07 | for i = 1 ,seconds.Value do --Use a for loop instead of a while loop |
08 | wait( 1 ) |
09 | seconds.Value = seconds.Value - 1 |
10 | script.Parent.Text = seconds.Value |
11 | end |
12 | end ) |