Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago
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)

There is an error : 12:12:01.818 - Workspace.Part.Script:8: attempt to index local 'Player' (a nil value) This is a server script BTW

1 answer

Log in to vote
0
Answered by
compUcomp 417 Moderation Voter
4 years ago

Touched events don't always fire on player characters. Make sure it's a character, then get the gui.

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) 

if oGUI and Player then
    local PlayerGui = Player.PlayerGui 
    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)


0
Once you fix that, tell me if there are any other errors, and I'll edit my post to fix them. compUcomp 417 — 4y
0
Also, you can't access things inside of PlayerGui from a server script, you can put GUI into it, but you can't remove anything or see things in it MrLonely1221 701 — 4y
1
Not entirely correct ^. If the server either clones or creates a child and assigns its Parent to a player's PlayerGui, the server can access that child, but it still can't access anything else in the PlayerGui. DeceptiveCaster 3761 — 4y
Ad

Answer this question