Remote Event firing to a disabled script

I fire a remote event from client to server when I press E. The script that receives this event is disabled at first and nothing happens, but when it gets enabled, all the events that were fired previously get connected at once. I set a condition that script.Disabled == false and it still doesn’t work. I made a simple script for testing:

event.OnServerEvent:Connect(function()
print("fired")
end)

Basically once this script gets enabled, it prints “fired” as many times as I’ve pressed E while it was disabled.

3 Likes

That’s because the callback function is connected to the event before the script is disabled.

I’ve already tried it and it didn’t work.

You wouldn’t be able to check if a server script was disabled or not from the client side.

I don’t think I understand. The script is stored in ServerScriptService and Disabled = true from the start.

Are you disabling the server script locally from a local script? That’ll explain why it’s still executing.


local EventFired = false

event.OnServerEvent:Connect(function()
if EventFired == false then
EventFired = true
print("fired")
end)
1 Like

It has Disabled set to true from the beginning.
Here’s the original script maybe that situation is different from the test:

local function toBread1()
	if script.Disabled == false then
		local player, item = event.OnServerEvent:Wait()
		if item.Name == "Bun" then
			local toTake = serverStorage.Items.Bun:Clone()
			toTake.Parent = workspace
			player.Character.Humanoid:EquipTool(toTake)
			return
		else
			toBread1()
		end
	end
end
toBread1()

So here I get the tools equipped once the script gets enabled if I have clicked E before.

What exactly are you trying to do in the first place? Why do you need the script to be disabled initially?

It gets cloned into a part and then enabled.

Did you try my debounce code or did it not work?

I did and it didn’t unfortunately.

Are you sure it isn’t the script in that part which is being fired?

You didn’t answer my question, what do you mean by

What are you trying to achieve in the first place?

This is a script that tells the player to go to certain places and take items to complete a cooking recipe. Every time a new plate appears it gets a recipe script cloned into it from serverStorage. That’s why it’s disabled at first.

I don’t see what do you mean by that.

Full LocalScript:

local restaurant = game.Workspace:WaitForChild("Restaurant")
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local UIS = game:GetService("UserInputService")
local playerPosition
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.PickUp
local interactables = restaurant.Storage:GetChildren()
local function checkInteractables()
	interactables = restaurant.Storage:GetChildren()
end
restaurant.Storage.ChildAdded:Connect(checkInteractables)
restaurant.Storage.ChildRemoved:Connect(checkInteractables)

UIS.InputBegan:Connect(function(input, gameProcessed)
	for i,v in pairs(interactables) do	
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.E and v.PrimaryPart.E.Enabled == true and (playerPosition - v.PrimaryPart.Position).magnitude <= 10 then
				event:FireServer(v.PrimaryPart)
			end
		end
	end
end)

game:GetService("RunService").Heartbeat:Connect(function()
	local closest
	playerPosition = character.PrimaryPart.Position
	if unpack(interactables) ~= nil  then
		for i,v in pairs(interactables) do
			if closest == nil then
				closest = v
			else
				if (playerPosition - v.PrimaryPart.Position).magnitude < (closest.PrimaryPart.Position - playerPosition).magnitude then
					closest = v
				end
			end
		end
	end
	if closest ~= nil then
		for i,v in pairs(interactables) do
			if v == closest and (playerPosition - v.PrimaryPart.Position).magnitude <= 10 then
				v.PrimaryPart.E.Enabled = true
			else
				v.PrimaryPart.E.Enabled = false
			end
		end
	end
end)

Full ServerScript that is disabled at first and gets enabled once it’s parented into a workspace model:

local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.PickUp
local storage = game.Workspace.Restaurant.Storage
local serverStorage = game:GetService("ServerStorage")
local plate = script.Parent
local indicator = serverStorage.Indicator:Clone()

------------------------------------------------
indicator.Parent = game.Workspace.Restaurant.Room
local function toBread1()
	if script.Disabled == false then
		local player, item = event.OnServerEvent:Wait()
		if item.Name == "Bun" then
			local toTake = serverStorage.Items.Bun:Clone()
			toTake.Parent = workspace
			player.Character.Humanoid:EquipTool(toTake)
			return
		else
			toBread1()
		end
	end
end
toBread1()
------------------------------------------------
indicator.Parent = game.Workspace.Restaurant.Room
local function toTomato()
	if script.Disabled == false then
		local player, item = event.OnServerEvent:Wait()
		if item.Name == "Tomato" then
			local toTake = serverStorage.Items.Tomato:Clone()
			toTake.Parent = workspace
			player.Character.Humanoid:EquipTool(toTake)
			return
		else
			toTomato()
		end
	end
end
toTomato()

indicator.Parent = plate.PrimaryPart
local function weld1()
	if script.Disabled == false then
		local player, item = event.OnServerEvent:Wait()
		if item == plate.PrimaryPart then
			player.Character.Humanoid:UnequipTools()
			local toWeld = serverStorage.Ingredients.FlatBun:Clone()
			toWeld.Parent = plate
			player.Backpack.Bun:Destroy()
			toWeld.CFrame = (plate.Plate.CFrame + Vector3.new(0,0.075,0))
			local weld = Instance.new("WeldConstraint")
			weld.Parent = plate.Plate
			weld.Part0 = plate.Plate
			weld.Part1 = toWeld
			return
		else
			weld1()
		end
	end
end
weld1()

indicator.Parent = plate.PrimaryPart
local function weld2()
	if script.Disabled == false then
		local player, item = event.OnServerEvent:Wait()
		if item == plate.PrimaryPart then
			player.Character.Humanoid:UnequipTools()
			local toWeld = serverStorage.Ingredients.TomatoSlice:Clone()
			toWeld.Parent = plate
			player.Backpack.Tomato:Destroy()
			toWeld.CFrame = (plate.Plate.CFrame + Vector3.new(0,0.275,0))
			local weld = Instance.new("WeldConstraint")
			weld.Parent = plate.Plate
			weld.Part0 = plate.Plate
			weld.Part1 = toWeld
			return
		else
			weld2()
		end
	end
end
weld2()

indicator.Parent = game.Workspace.Restaurant.Room
local function toBread2()
	if script.Disabled == false then
		local player, item = event.OnServerEvent:Wait()
		if item.Name == "Bun" then
			local toTake = serverStorage.Items.Bun:Clone()
			toTake.Parent = workspace
			player.Character.Humanoid:EquipTool(toTake)
			return
		else
			toBread2()
		end
	end
end
toBread2()

indicator.Parent = plate.PrimaryPart
local function weld3()
	if script.Disabled == false then
		local player, item = event.OnServerEvent:Wait()
		if item == plate.PrimaryPart then
			player.Character.Humanoid:UnequipTools()
			local toWeld = serverStorage.Ingredients.Bun:Clone()
			toWeld.Parent = plate
			player.Backpack.Bun:Destroy()
			toWeld.CFrame = plate.Plate.CFrame * CFrame.Angles(0,0,math.rad(180)) + Vector3.new(0,0.572,0)
			local weld = Instance.new("WeldConstraint")
			weld.Parent = plate.Plate
			weld.Part0 = plate.Plate
			weld.Part1 = toWeld
			indicator.Parent = nil
			return
		else
			weld3()
		end
	end
end
weld3()

plate.Ready.Value = true

I think I fixed it by adding this to the beginning of ServerScript.

event.OnServerEvent:Connect(function()
	if script.Disabled == true then
		player, item = nil, nil
	end
end)
local function toBread1()
	player, item = event.OnServerEvent:Wait()
	if item then
		if item.Name == "Bun" then
			local toTake = serverStorage.Items.Bun:Clone()
			toTake.Parent = workspace
			player.Character.Humanoid:EquipTool(toTake)
			return
		else
			toBread1()
		end
	else
		toBread1()
	end
end
toBread1()

So every time the event fires but the script is disabled the parameters passed from the event are set to nil and the script won’t proceed.

2 Likes