Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Script still functions after a local script destroys it, Why and how do i fix it?

Local Script:

local PointLight = game.Workspace.Lamp.Union.PointLight
local battery = 60
local lamp = game.Workspace.Lamp.Union
local timer = script.Parent.Timer


while true do
	wait()
	if PointLight.Enabled == true then
		battery = battery - 1
		print(battery)
		timer.Text = battery
		wait(1)
	end
	if battery == 1 then
		timer.Text = 0
		print("Lamp Break")
		lamp.PointLight:Destroy()
		lamp.Break:Play()
		lamp.Script.Disabled = true
		lamp.Material = 'Sand'
		lamp.Script:Destroy()
		wait(0.1)
		break
	end
end

Script That I Destroy:

local battery = 100

function onClicked()
	if battery ~= 0 then
		if script.Parent.PointLight.Enabled == false then
			script.Parent.Sound:Play()
			script.Parent.PointLight.Enabled = true
			script.Parent.Material = 'Neon'
		elseif script.Parent.PointLight.Enabled == true then
			script.Parent.Sound:Play()
			script.Parent.PointLight.Enabled = false
			script.Parent.Material = 'Sand'
		end
	end
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)

1 comment

0

I am not a scripting god so i don't know but does the upper local script work? not the script you destroy the local script?and in the script you destroy i don't see a Destroy()

1 answer

You cannot change a serverscript inside a localscript because of filtering enabled. Create a RemoteEvent in the ReplicatedStorage. So in your local script, fire the remote event in the ReplicatedStorage and destroy the script itself in the serverscript. Do these instead.

LocalScript:

local PointLight = game.Workspace.Lamp.Union.PointLight
local battery = 60
local lamp = game.Workspace.Lamp.Union
local timer = script.Parent.Timer


while true do
    wait()
    if PointLight.Enabled == true then
        battery = battery - 1
        print(battery)
        timer.Text = battery
        wait(1)
    end
    if battery == 1 then
        timer.Text = 0
        print("Lamp Break")
        lamp.PointLight:Destroy()
        lamp.Break:Play()
        lamp.Script.Disabled = true
        lamp.Material = 'Sand'
        game.ReplicatedStorage.RemoteEvent:FireServer()
        wait(0.1)
        break
    end
end

Script:

local battery = 100

function onClicked()
    if battery ~= 0 then
        if script.Parent.PointLight.Enabled == false then
            script.Parent.Sound:Play()
            script.Parent.PointLight.Enabled = true
            script.Parent.Material = 'Neon'
        elseif script.Parent.PointLight.Enabled == true then
            script.Parent.Sound:Play()
            script.Parent.PointLight.Enabled = false
            script.Parent.Material = 'Sand'
        end
    end
end

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
	script:Destroy()
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.