Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does the GUI and Sound keep on going in this server script ?


local Main = script.Parent 
local Players = game:GetService("Players") 
local oGUI = script:FindFirstChild('ScreenGui') 

Main.Touched:Connect(function(Hit)
local Player = Players:GetPlayerFromCharacter(Hit.Parent) 
local PlayerGui = Player.PlayerGui 

if oGUI and Player then
	Main.Sound:Play()
	for n = 0 , 2 , 0.2 do
    Main.Sound.Volume = n
  print('Hi!')
  local GUI = oGUI:Clone() 
   GUI.Parent = PlayerGui 
   wait(3)
   GUI:Destroy()
   for y = 2 , 0 , 0.2 do
	Main.Sound.Volume = y 
	wait(0)
	Main.Sound.Volume:Stop()
   end 
  end
 end
end)

I'm trying to make a thing where if you touch it a GUI pops up and it plays a song PS : script is a server script

2 comments

0

Can you tell us what is going wrong? What is happening that you don't want? And do you get any errors?

0

Testing it again there was an error , 10:59:53.284 - Workspace.Part.Script:8: attempt to index local 'Player' (a nil value)

1 answer

✓ Accepted answer

Line 18 is incorrect:

for y = 2, 0, 0.2 do

Now for the fun part: explaining what you did wrong.

Think of the parameters of a numeric for loop this way:

for var = x, y, z do

There are certain conditions that require z to be a specific number:

  • If x < y, z must be positive.

  • If x > y, z must be negative so that the loop counts down instead of up.

Because x > y is true for your parameters, your parameter z must receive a negative number as an argument. Because z is positive, the loop does not run because it cannot count up to 0 from 2. Basic logic.

To fix the issue, simply make z negative:

for y = 2, 0, -0.2 do

This discussion is preserved as it was. New answers, comments, and votes are closed.