Event fires once, function fires twice

I’m trying to make it so that when a player clicks on a button, it copies their player into my character customizer. Issue is, while the button fires once, the function goes off twice. I have checked, and there are no duplicate scripts.

Here’s the output:
image

The script that handles said button:

local Teams = script.Parent
local Human = Teams.Human
local Vamp = Teams.Vamp
local Wolf = Teams.Wolf
local Custom = Teams.Customizer
local Cams = game.Workspace.Cameras
local Cam = game.Workspace.Camera
local TeamSetter = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("SetTeam")
local CloseEvent = game.ReplicatedStorage.Events:WaitForChild("CloseTitle")
local Loaded = game.ReplicatedStorage.Events:WaitForChild("LoadedIn")
local CharEdit = game.ReplicatedStorage.Events:WaitForChild("CharEdit")
local cas = game:GetService("ContextActionService")
local plr = game:GetService("Players").LocalPlayer
local playerGui = plr:WaitForChild("PlayerGui")
local char = plr.Character
local Title = playerGui:WaitForChild("Title")
local DB = false

local blur = require(game.Lighting.Blur.Animator)
local tran = require(game.Lighting.Transition.Animator)

local function transition()
	blur.Transition:Play()
	tran.Transition:Play()
end

plr.CharacterAdded:Connect(function()
	DB = false
	char = plr.Character
	TeamSetter:FireServer("Spirits")
	cas:BindActionAtPriority("DisableControls",function()
		return Enum.ContextActionResult.Sink
	end, false, Enum.ContextActionPriority.High.Value, unpack(Enum.PlayerActions:GetEnumItems()))
	print("freeze")
	Cam.CameraType = Enum.CameraType.Scriptable
	Cam.CFrame = Cams.Human.CFrame
	Title.Enabled = true
end)

Human.MouseEnter:Connect(function()
	if DB == false then
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.Human.CFrame
		transition()
	end
end)

Human.Activated:Connect(function()
	if DB == false then
		DB = true
		TeamSetter:FireServer("Humans", Cams.Human)
		Cam.CameraType = Enum.CameraType.Custom
		Cam.CameraSubject = char.Humanoid
		CloseEvent:Fire()
		cas:UnbindAction("DisableControls")
	end
end)

Vamp.MouseEnter:Connect(function()
	if DB == false then
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.Vampire.CFrame
		transition()
	end
end)

Vamp.Activated:Connect(function()
	if DB == false then
		DB = true
		TeamSetter:FireServer("Vampires", Cams.Vampire)
		Cam.CameraType = Enum.CameraType.Custom
		Cam.CameraSubject = char.Humanoid
		CloseEvent:Fire()
		cas:UnbindAction("DisableControls")
	end
end)

Wolf.MouseEnter:Connect(function()
	if DB == false then
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.Werewolf.CFrame
		transition()
	end
end)

Wolf.Activated:Connect(function()
	if DB == false then
		DB = true
		TeamSetter:FireServer("Werewolves", Cams.Werewolf)
		Cam.CameraType = Enum.CameraType.Custom
		Cam.CameraSubject = char.Humanoid
		CloseEvent:Fire()
		cas:UnbindAction("DisableControls")
	end
end)

Custom.Activated:Connect(function()
	if DB == false then
		print(DB)
		DB = true
		CharEdit:Fire()
		print("fired")
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.CharEdit.CFrame
	end
end)

Loaded.Event:Connect(function()
	DB = false
end)

The script with the function that clones the character.

local LoadedIn = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("LoadedIn")
local CloseEvent = game.ReplicatedStorage.Events:WaitForChild("CloseTitle")
local Title = game.Players.LocalPlayer.PlayerGui:WaitForChild("CharCustom")
local AniModule = require(Title:WaitForChild("Frame"):WaitForChild("UIGradient"):WaitForChild("Animator"))
local FadeIn = AniModule.FadeIn
local FadeOut = AniModule.FadeOut
local In = false
local CharEdit = game.ReplicatedStorage.Events:WaitForChild("CharEdit")
local Room = game.ReplicatedStorage:WaitForChild("ClientMaps"):WaitForChild("CustomEditor")
local Player = game.Players.LocalPlayer
local part = game.Workspace.SpawnLocation
local fakeChar
local SetFake = game.ReplicatedStorage.Events:WaitForChild("SetFake")

CharEdit.Event:Connect(function()
	print("AAAAA")
	Room.Parent = game.Workspace
	Title.Enabled = true
	FadeIn:Play()
	In = true
	Room.Parent = game.Workspace
	Player.Character.Archivable = true
	fakeChar = Player.Character:Clone()
	fakeChar.HumanoidRootPart.Position = Vector3.new(-587.5, 62.656, -84)
	fakeChar.HumanoidRootPart.Orientation = Vector3.new(0, -90, -0)
	fakeChar.Parent = game.Workspace
	fakeChar.Name = "FakeChar"
	Player.Character.Archivable = false
	SetFake:Fire(fakeChar)
end)

LoadedIn.Event:Connect(function()
	FadeOut:Play()
	task.wait(1.5)
	Title.Enabled = false
	Room.Parent = game.ReplicatedStorage.ClientMaps
end)

(Yes, Ik this is very spaghetti code-y, I’ll work on that once I get it to even function.)

4 Likes

Try deleting the event and see where it errors (first fire), then try deleting the event immediately after you fire it in your code to see if it errors then (second fire?). I really can’t tell what’s going on here because from what’s posted it should be working. It has to be 2 fires though somehow.

2 Likes

Try using the find all feature, for all scripts, and search for the event there.

(Keybind: CTRL + Shift + F)

2 Likes

I did, there was only one.


1 Like

Try printing before the event fires, so we can see if it is called twice or a Roblox issue.

2 Likes

I also usually get this issue on my scripts and the way that i fixed it by adding a boolean to check if the remoteEvent fired, it’s something similar like

Custom.Activated:Connect(function()
    local fired = false
	if DB == false then
		print(DB)
		DB = true
        if fired == false then
		    CharEdit:Fire()
            fired = true
        end
		print("fired")
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.CharEdit.CFrame
	end
end)

Which could probably fix the issue that you’re having

2 Likes

Instead of doing CharEdit:Fire(), search for all scripts that use the event, you can do this by tying in the name of the event.

2 Likes

image
Those are all scripts that use it. None are duplicates
image
All use the function, but do different things, I just checked.
I also did the check before the event fires.
image

	print("ey homeboy")
	local fired = false
	if DB == false then
		print(DB)
		DB = true
		if fired == false then
			CharEdit:Fire()
			fired = true
		end
		print("fired")
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.CharEdit.CFrame
	end
end)
1 Like

Did it print before any of the other if DB == false then lines, since there are three different events that then cause this one to fire?

2 Likes

did that, no other events were fired.

local Teams = script.Parent
local Human = Teams.Human
local Vamp = Teams.Vamp
local Wolf = Teams.Wolf
local Custom = Teams.Customizer
local Cams = game.Workspace.Cameras
local Cam = game.Workspace.Camera
local TeamSetter = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("SetTeam")
local CloseEvent = game.ReplicatedStorage.Events:WaitForChild("CloseTitle")
local Loaded = game.ReplicatedStorage.Events:WaitForChild("LoadedIn")
local CharEdit = game.ReplicatedStorage.Events:WaitForChild("CharEdit")
local cas = game:GetService("ContextActionService")
local plr = game:GetService("Players").LocalPlayer
local playerGui = plr:WaitForChild("PlayerGui")
local char = plr.Character
local Title = playerGui:WaitForChild("Title")
local DB = false

local blur = require(game.Lighting.Blur.Animator)
local tran = require(game.Lighting.Transition.Animator)

local function transition()
	blur.Transition:Play()
	tran.Transition:Play()
end

plr.CharacterAdded:Connect(function()
	print("DB exists")
	DB = false
	char = plr.Character
	TeamSetter:FireServer("Spirits")
	cas:BindActionAtPriority("DisableControls",function()
		return Enum.ContextActionResult.Sink
	end, false, Enum.ContextActionPriority.High.Value, unpack(Enum.PlayerActions:GetEnumItems()))
	print("freeze")
	Cam.CameraType = Enum.CameraType.Scriptable
	Cam.CFrame = Cams.Human.CFrame
	Title.Enabled = true
end)

Human.MouseEnter:Connect(function()
	print("e")
	if DB == false then
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.Human.CFrame
		transition()
	end
end)

Human.Activated:Connect(function()
	print("the internal screaming is real")
	if DB == false then
		DB = true
		TeamSetter:FireServer("Humans", Cams.Human)
		Cam.CameraType = Enum.CameraType.Custom
		Cam.CameraSubject = char.Humanoid
		CloseEvent:Fire()
		cas:UnbindAction("DisableControls")
	end
end)

Vamp.MouseEnter:Connect(function()
	print("i think this might be roblox's fault")
	if DB == false then
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.Vampire.CFrame
		transition()
	end
end)

Vamp.Activated:Connect(function()
	print("help")
	if DB == false then
		DB = true
		TeamSetter:FireServer("Vampires", Cams.Vampire)
		Cam.CameraType = Enum.CameraType.Custom
		Cam.CameraSubject = char.Humanoid
		CloseEvent:Fire()
		cas:UnbindAction("DisableControls")
	end
end)

Wolf.MouseEnter:Connect(function()
	print("hey guys, it's markiplier")
	if DB == false then
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.Werewolf.CFrame
		transition()
	end
end)

Wolf.Activated:Connect(function()
	print("heloo")
	if DB == false then
		DB = true
		TeamSetter:FireServer("Werewolves", Cams.Werewolf)
		Cam.CameraType = Enum.CameraType.Custom
		Cam.CameraSubject = char.Humanoid
		CloseEvent:Fire()
		cas:UnbindAction("DisableControls")
	end
end)

Custom.Activated:Connect(function()
	print("ey homeboy")
	local fired = false
	if DB == false then
		print(DB)
		DB = true
		if fired == false then
			CharEdit:Fire()
			fired = true
		end
		print("fired")
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.CharEdit.CFrame
	end
end)

Loaded.Event:Connect(function()
	DB = false
end)

image

1 Like

Try passing a parameter and printing it on the receiving end of the event, just to confirm it is coming from Custom.Activated

2 Likes

image

Custom.Activated:Connect(function()
	print("ey homeboy")
	local fired = false
	if DB == false then
		print(DB)
		DB = true
		if fired == false then
			CharEdit:Fire("Custom.Activated")
			fired = true
		end
		print("fired")
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = Cams.CharEdit.CFrame
	end
end)
CharEdit.Event:Connect(function(tthing)
	print("AAAAA")
	print(tthing)
	Room.Parent = game.Workspace
	Title.Enabled = true
	FadeIn:Play()
	In = true
	Room.Parent = game.Workspace
	Player.Character.Archivable = true
	fakeChar = Player.Character:Clone()
	fakeChar.HumanoidRootPart.Position = Vector3.new(-587.5, 62.656, -84)
	fakeChar.HumanoidRootPart.Orientation = Vector3.new(0, -90, -0)
	fakeChar.Parent = game.Workspace
	fakeChar.Name = "FakeChar"
	Player.Character.Archivable = false
	SetFake:Fire(fakeChar)
end)
1 Like

At this point, and for a lack of a better term, I am bamboozled, it looks like the function fires twice, even though the code fires it once, you might want to consider putting this topic to #bug-reports:studio-bugs.

2 Likes

I would, but unfortunately I don’t have access to making topics in bug reports… I joined after they closed the program that let devs get access to it. I’ll try flagging for a category change, though it’s unlikely to work.

3 Likes

I should mention, this all started happening after deleting Rojo (it was clogging up script analysis). I don’t know if that caused it. I’ll reinstall studio.

1 Like

Happened in a fresh install of Studio. I’ll test it in the Roblox Player

1 Like


Nope. Not Studio exclusive.

1 Like

Modified the code to check if a clone is already there, still creates two clones.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LoadedIn = ReplicatedStorage:WaitForChild("Events"):WaitForChild("LoadedIn")
local CloseEvent = ReplicatedStorage.Events:WaitForChild("CloseTitle")
local SetFake = ReplicatedStorage.Events:WaitForChild("SetFake")

local Players = game.Players
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Title = PlayerGui:WaitForChild("CharCustom")

local Frame = Title:WaitForChild("Frame")
local UIGradient = Frame:WaitForChild("UIGradient")
local AniModule = require(UIGradient:WaitForChild("Animator"))
local FadeIn = AniModule.FadeIn
local FadeOut = AniModule.FadeOut

local In = false

local CharEdit = ReplicatedStorage.Events:WaitForChild("CharEdit")
local ClientMaps = ReplicatedStorage:WaitForChild("ClientMaps")
local Room = ClientMaps:WaitForChild("CustomEditor")

local Workspace = game.Workspace
local part = Workspace.SpawnLocation
local fakeChar

CharEdit.Event:Connect(function(tthing)
	if fakeChar ~= true then
		print("AAAAA")
		print(tthing)
		Room.Parent = game.Workspace
		Title.Enabled = true
		FadeIn:Play()
		In = true
		Room.Parent = game.Workspace
		Player.Character.Archivable = true
		fakeChar = Player.Character:Clone()
		fakeChar.HumanoidRootPart.Position = Vector3.new(-587.5, 62.656, -84)
		fakeChar.HumanoidRootPart.Orientation = Vector3.new(0, -90, -0)
		fakeChar.Parent = game.Workspace
		fakeChar.Name = "FakeChar"
		Player.Character.Archivable = false
		SetFake:Fire(fakeChar)
		fakeChar = true
	end
end)

LoadedIn.Event:Connect(function()
	FadeOut:Play()
	task.wait(1.5)
	Title.Enabled = false
	Room.Parent = game.ReplicatedStorage.ClientMaps
	fakeChar = false
end)
1 Like

From the icon in this image, it appears that you are using Scripts with the RunContext set to Client. This means that they can run anywhere, even in StarterGui. The contents of StarterGui always exist, and they are cloned to the client’s PlayerGui upon loading into the game (or respawning, if the child is not a LayerCollector or has ResetOnSpawn enabled). Normally, this would not matter, as LocalScripts do not run in StarterGui. However, RunContext allows the Script to run anywhere, meaning that two copies of the script are running, one in StarterGui and one in the PlayerGui.

To get around this, you should always use LocalScripts in all ‘Starter’ containers, which is the recommended option in the announcement for RunContext.

2 Likes

Hm… Tried switching to localscripts, nothing changed.

1 Like