hi there im trying to make a script that when someone clicks on a gui it closes it and resets them the close gui part is fine the kill part isnt. code:
local function onClick()
local player = game:GetService("Players").LocalPlayer
script.Parent.Parent.Visible = false
for i = 1, 0, -0.1 do
script.Parent.Parent.Parent.Music.Volume = i
wait(0.07)
end
script.Parent.Parent.Parent.Music:Stop()
end
script.Parent.MouseButton1Click:Connect(onClick)
script.Parent.PlayButton.MouseButton1Click:Connect(onClick)
player:LoadCharacter()
This means there must be an error in the line that defines the âplayerâ variable.
I find that the game sometimes doesnât like it when you use a function to retrieve something and try to access it in the same line, try it in two lines like so:
local Players = game:GetService(âPlayersâ)
local Player = Players.LocalPlayer
If you only want to use one variable to do it you can just do it like this
local Player = game:GetService(âPlayersâ)
Player = Player.LocalPlayer â Replaces The Last Value With LocalPlayer
local function onClick()
local players = game:GetService("Players")
local player = players.LocalPlayer
script.Parent.Parent.Visible = false
for i = 1, 0, -0.1 do
script.Parent.Parent.Parent.Music.Volume = i
wait(0.07)
end
script.Parent.Parent.Parent.Music:Stop()
end
script.Parent.MouseButton1Click:Connect(onClick)
script.Parent.PlayButton.MouseButton1Click:Connect(onClick)
player.Character.Humanoid.Health = 0
Your current script has the line outside of the function, and because the player is defined inside of the scope of the function, it has to be inside the function. Try this:
local function onClick()
local players = game:GetService("Players")
local player = players.LocalPlayer
script.Parent.Parent.Visible = false
for i = 1, 0, -0.1 do
script.Parent.Parent.Parent.Music.Volume = i
wait(0.07)
end
script.Parent.Parent.Parent.Music:Stop()
player.Character.Humanoid.Health = 0
end
script.Parent.MouseButton1Click:Connect(onClick)
script.Parent.PlayButton.MouseButton1Click:Connect(onClick)
player is highlighted in red meaning it thinks the variable has not been defined , if you copied what I showed you make sure the variables are exactly the same, it is case sensitive
local players = game:GetService("Players")
local player = players.LocalPlaye
local function onClick()
script.Parent.Parent.Visible = false
for i = 1, 0, -0.1 do
script.Parent.Parent.Parent.Music.Volume = i
wait(0.07)
end
script.Parent.Parent.Parent.Music:Stop()
end
script.Parent.MouseButton1Click:Connect(onClick)
script.Parent.PlayButton.MouseButton1Click:Connect(onClick)
player.Character.Humanoid.Health = 0