Is this possible to do in a server script?

How can you delete a local script thats inside the players character with a server script? Would you have to use :Destroy()? or something else? since i tried :Destroy() and it didnt seem to work, is there another way to do it?

6 Likes

Yes, since the Localscript is an instance you would use :Destroy()

8 Likes

Yep you can its an instance so either destroy, or there’s a property named enabled you can try to turn that off as well.

5 Likes

but i get this error whenever i try it


i dont know why it happens, this is the script for it

4 Likes

it works when i try to destroy a server script but doesnt work when its a local script getting destroyed, why does this happen?

2 Likes

So the local script doesn’t get destroyed, but does the bike:Destroy() work?

2 Likes

Try disabling it instead of destroying it.

2 Likes

its the same for when i want to disable it, it just comes back as nil like its not even there when it clearly is

2 Likes

bike:Destroy() works but the destroying of the local script doesnt work

2 Likes

Make sure the local script is in StarterCharacterScripts and not StarterPlayerScripts

3 Likes

I put the placed both the bike and the script inside the players character and the script destroying still doesnt seem to work

3 Likes

I think i know why. Earlier in the script you’re already defining ChosenAbility and then trying to say the local script is FindFirstChild(ChosenAbility.Name)

Instead do

local ability = char:FindFirstChild(“ChosenAbility”)

Or you can change the name of the variable called ChosenAbility

4 Likes

Thats also probably why its saying you’re attempting to index nil.

4 Likes

I removed the .Name in the chosen ability pathway variable and still got the same error with a new one

3 Likes

Look, here is how i think you can fix your problem:

First, change the variable to something like

local CurrentAbility = Abilities:FindFirstChild("CurrentAbility").Value

then, change the local script variable to

local ability = char:FindFirstChild("ChosenAbility")

This is because whenever you use a FindFirstChild or WaitForChild you always use parenthesis and then quotation marks. Like so:

local example = game.ReplicatedStorage:FindFirstChild("Example").Value -- The .Name, or .Value or whatever it may be always comes after the ("")
3 Likes

A server script normally replicates everything for every connected client. If you delete only one local script from one specific client, you must use a RemoteEvent to go from the server to the client.

To do this, fire the RemoteEvent from the server on the event when you want it to delete the script.

RemoteEvent:FireClient()

Then, on the receiving client, make it find the script & delete it afterward!

RemoteEvent.OnClientEvent:Connect(function()
    local Player = game.Players.LocalPlayer
    local LocalScript = Player:FindFirstChildOfClass("LocalScript"):GetFullName("LocalScript")
    
    LocalScript:Destroy()
end)
4 Likes

I sent a remote event to the client to destroy the script, and got this error


this is the server script


this is the client script

2 Likes

shouldn’t the local script be `

local LocalScript = script.Parent

or should it be:

       local char = player.Character
       local LocalScript = char.LocalScript
3 Likes

But when i use that, it only makes a pathway to one specific thing all the time and i dont want that, in my case the player can change the current cart/ current ability value in my game and it wont always be the same so when i the local variable instead of quotation marks it can adapt to its changed value, for example the current cart value could be a bike and so it spawns a bike or the current cart value could be a sleigh and it spawns a sleigh

4 Likes

Ah, my bad. i didn’t know the context, sorry!

4 Likes