The local script does not start

Hello, this is a small project that despite attempts I have not been able to fix.

  1. What do you want to achieve? Keep it simple and clear!

That the objects are destroyed when the Local Script is duplicated, this will cause it to be destroyed when the object is touched

  1. What is the issue? Include screenshots / videos if possible!

The local script does not run, what I have investigated is that it only runs when the game starts but I don’t know how to get it to run after

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    The first one works well because it is executed just when the game starts, but when it is destroyed the other one is automatically generated and that is the one that does not work

here is the code that duplicates it

local WhileActivated = true
local WhileActivated2 = true
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EggHuntEventsFolder = ReplicatedStorage:WaitForChild("EggHuntEvents")
local Egg = script.Parent

local EventControl = game.StarterPlayer.StarterPlayerScripts.EggsHuntLocalScripts.EventsControl.EventControlForClone:Clone()
EventControl.Parent = game.StarterPlayer.StarterPlayerScripts
EventControl.Enabled  = true

while WhileActivated2 do
	wait(1)
	if not EggHuntEventsFolder:FindFirstChild("RemoteForEggHunt") then
		local EventControl = game.StarterPlayer.StarterPlayerScripts.EggsHuntLocalScripts.EventsControl.EventControlForClone:Clone()
		EventControl.Parent = game.StarterPlayer.StarterPlayerScripts
		EventControl.Enabled  = true
		WhileActivated2 = false
	end
end

while WhileActivated do
	wait()
	if script.Parent.Parent.Parent == workspace then
--		print("InWorkspace")
		local NewRemoteEvent = Instance.new("RemoteEvent")
		NewRemoteEvent.Parent = EggHuntEventsFolder
		NewRemoteEvent.Name = "RemoteForEggHunt"
		
		local StringValue = Instance.new("StringValue")
		StringValue.Parent = NewRemoteEvent
		StringValue.Name = "NameOfEgg"
		StringValue.Value = script.Parent.Name
		
		
		Egg.Touched:Connect(function(hit)
			print("Touched")
			local Humanoid = hit.Parent:FindFirstChild("Humanoid")
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if Humanoid and EventControl.Enabled == true then
				print("Humanoid")
				print(NewRemoteEvent)
				NewRemoteEvent:FireClient(Player)
			end
		end)
		
		WhileActivated = false
	end
end

this is the local script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.EggHuntEvents:WaitForChild("RemoteForEggHunt")


Event.OnClientEvent:Connect(function()
	print("Event")
	local NameOfEgg = Event:WaitForChild("NameOfEgg")
	print(NameOfEgg)
	local FoundEgg = game.Workspace.Eggs:FindFirstChild(NameOfEgg.Value)
--	print(FoundEgg)
	--print(NameOfEgg)
	FoundEgg:Destroy()
	Event:Destroy()
	script:Destroy()
end)

I hope you can help me
Thank you!

Hi!! Where is your LocalScript located?

Remember that LocalScripts won’t run if they’re a descendant of the Workspace. (Unless in character models)

See here for a table of valid container services from which LocalScripts will execute.

Hope this help you! Good luck :]

1 Like

What is it showing for errors?

I believe your issue is the first two statements

whileactivated = true, simply because what is whileactivated

Because later you are basically saying

while true do

1 Like

Hello! is located in StarterPlayer and the local script to be cloned is located in StarterPlayer-StarterPlayerScripts

Hello! I put this in to prevent so many events from being created at the same time so I can constantly check until the parent is the workspace and no errors occur in the console.

It looks like this part of the script is continuously creating a RemoteEvent named “RemoteForEggHunt” and placing it in EggHuntEventsFolder on ReplicatedStorage.

while WhileActivated do
	wait()
	if script.Parent.Parent.Parent == workspace then
		--		print("InWorkspace")
		local NewRemoteEvent = Instance.new("RemoteEvent")
		NewRemoteEvent.Parent = EggHuntEventsFolder
		NewRemoteEvent.Name = "RemoteForEggHunt"

-- rest of code

In the same continuously running while loop you are checkng for the same NewRemoteEvent.

Since the RemoteEvent is being created over and over how does the Egg.Touch function know which one to look for?

1 Like

Not sure exactly what your goal is, but you can have a single script for a bunch of parts by doing something like this:

local EggsFolder = "WhereYouHaveTheEggs"
local NewRemoteEvent = "LocationOnReplicatedStorage"

local function GatherEggs()
	
	for _, egg in pairs(EggsFolder:GetChildren()) do
		
		if egg and egg:IsA("BasePart") then -- assuming they are BaseParts since you are using a touched event
			
			egg.Touched:Connect(function()
				print("Touched")
				local Humanoid = hit.Parent:FindFirstChild("Humanoid")
				local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if Humanoid and EventControl.Enabled == true then
					print("Humanoid")
					print(NewRemoteEvent)
					NewRemoteEvent:FireClient(Player)
				end				
			end)
		end
		
	end
end

EggsFolder.ChildAdded:Connect(function() GatherEggs() end)

GatherEggs()

Each time the EggsFolder gets a new egg the function is called to apply the function to the new egg.

1 Like

Hello! It worked, thank you very much.

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