script:Destroy() not working

so what would the solution be exactly? (sorry for asking, its just not that obvious to me.)

Directly check the list without a PlayerAdded event.

one problem, now plr is an invalid variable without PlayerAdded.

Players.LocalPlayer is a property

im pretty sure that only works in local scripts, but ill try.

Are you using a server script??

yes because of me xd, @GoodGuy21938 go back to your original method. i misled you by accident

I found a simple solution. Sadly couldn’t think of anything else, but it works so… ¯_(ツ)_/¯

Here’s the code sample.
What I did here was put an assert
Which will force the code to error at that line and so it won’t run anything bellow.

The only two drawbacks is the random error that will appear(if you put an error message like in the example) and your script may have a little warning on the line underneath assert.

Other than that, I tested it, it destroyed the script and it also never printed “B”

local plr = game.Players.LocalPlayer
local whitelist = {"GoodGuy21938"}

if table.find(whitelist, plr.Name) then
	print("A") -- test
	task.wait(2) -- waiting a bit
	script:Destroy() -- KILL IT
	assert(false, "DIE DIE DIE") -- force error code
	print("B") -- print what? NEVER
end

Oh,yes i am. I was using a local script, then from the first reply I switched to a server script. I havent explicitly been told to switch back.

Well for your Freecam script you need to use the LocalScript. And LocalPlayer works there.

hmm… weird…
Screenshot 2023-10-04 191011


it did indeed, never print “B”.

have you tried restarting studio? when i had a weird thing happen once i restarted and it worked. like i dont see whats being done wrong here…

The code I’m assuming your talking about is underneath the if statement is running because when you destroy a script, you don’t stop the execution of that script - that’s just how it is. You could add a return to the end of the if statement to stop running what’s underneath. No need for assert!

1 Like

Wait wait wait
Freecam? That a custom or the roblox default one?
Are you trying to remove it from developers or add it inside others?

Freecam gets created for everyone who has…I believe Edit access to the place.
It seems to not be replaceable too, because I get 2 of those if I put it in StarterGui.
However the one I add to starter Gui has the modified code and it deletes itself as it’s supposed to.

To remove that you’d have to first find it within the playerGui and then delete it, which I believe shouldn’t let you use it anymore.

Otherwise to add it you’d just need to put it inside players when they appear.

it didnt destroy the script, but it worked!

Yeah, if you want to destroy the script you need to use script:Destroy(), return will just back out of the script’s execution.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.