My Gui script wont work, gui problems

Only part of my script works, im confused, when i press it doesnt print or change the values
to launch game i am using this:
dsfved

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Parent:Destroy() -- this is the only part that works
	
	local player = script.Parent.Parent.DisplayName.Text
	workspace.PlayersLeft[player].Value = "Dead"
	print(player.."has been killed.")
end)
1 Like

Could you show the hierarchy of the game while in a game?

1 Like

what do you mean by “hierarchy”

1 Like

The explorer on the right side of your screen.

1 Like

Sorry my computer crashed, here
sdvd

1 Like

Ur removing one of the ancestors of the script meaning when it depetes the ancestors the ancestors child would also delete including your script

1 Like

Your button is a descendant of the thing your destroying so the button gets destroyed aswell and your script which is inside it, your best choice is to make the :destroy() at the end of the function

script.Parent.MouseButton1Click:Connect(function()
	local player = script.Parent.Parent.DisplayName.Text
	workspace.PlayersLeft[player].Value = "Dead"
	print(player.."has been killed.")

    --destroy it after
    script.Parent.Parent.Parent:Destroy()
end)
1 Like

Basically, to dumb it down, you’ve kamikaze’d your script.
You’ve destroyed the script’s parent’s parent’s parent, (let’s call it the script’s great-grandparent) meaning everything inside that great-grandparent has been destroyed as well. Including the script you’re running.
So this means the last line of code the script will execute is

script.Parent.Parent.Parent:Destroy()

because the script isn’t even alive to execute the rest of the code.
make sense?

So, to fix it, you have to destroy the script’s great-grandparent last. You can do this by simply moving script.Parent.Parent.Parent:Destroy() to the bottom of your script like this:

script.Parent.MouseButton1Click:Connect(function()
	local player = script.Parent.Parent.DisplayName.Text
	workspace.PlayersLeft[player].Value = "Dead"
	print(player.."has been killed.")
    script.Parent.Parent.Parent:Destroy()
end)