My reset script doesnt work

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()

You want to kill the player, like set their Health to 0?

yes thats it :slight_smile: :+1:

Can’t you just do,

player.Character.Humanoid.Health = 0

it says W001: (18,1) Unknown global ‘player’

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

2 Likes

local function onClick(player)

If you are using RemoteEvent/function

1 Like

Its probably because you indexing player inside of a function. Try put local player it away from it

1 Like

ok i have tryed that it still says this image

Can you show us whole script again?

sure here:

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

Make the player variable outside the function.

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)
3 Likes

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

let me try this one out im not getting any errors from it

do this:

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

or this variant, it also fine and works

He wants the player to die when they press the button, that will kill them instantly as soon as the script runs.

1 Like

oh really?, ok so your script is right

Isnt the MouseButton firing the function?