Event not binding to function inside a pairs loop

I’m trying to get whatever gun a player has equipped to suppress when they interact with a proximityprompt. The problem is, whenever I trigger the suppression, nothing prints in the trigger function.

I’ve already created the code to make it so the player only has suppression when they equip the gun which they triggered for suppression.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local ACS_ENGINE = ReplicatedStorage.ACS_Engine
local GunModels = ACS_ENGINE.GunModels
local GrenadeModels = ACS_ENGINE.GrenadeModels

local Tools = ServerStorage.Tools
local clickTools = script.Parent
local Updates = clickTools.Updates

local Origin = workspace.Origin

local Distributer = require(script.Distributer)

local function Clicked(instance)
	return function(player)
		Distributer:Init(player,instance)
	end
end

local function Triggered(instance)
	return function(player)
		Distributer:Upgrade(player,instance)
	end
end

for i,tool in Tools:GetChildren() do
	local part = Instance.new("Part")
	local cd = Instance.new("ClickDetector")
	part.Size = Vector3.new(4, 2, 2)
	part.Position = Origin.Position + Vector3.new(0,0,i * 4)
	part.Name = tool.Name
	part.CanCollide = false
	part.Anchored = true
	part.Transparency = 1
	cd.Parent = part
	part.Parent = clickTools
	cd.MouseClick:Connect(Clicked(part))
end

for i,model in GunModels:GetChildren() do
	model.Archivable = true
	local newModel = model:Clone()
	local parent = clickTools:FindFirstChild(model.Name)
	newModel:FindFirstChild("Handle").Anchored = true
	newModel:PivotTo(parent.CFrame)
	newModel.Parent = parent
end

for i,v in Updates:GetChildren() do
	v.ProximityPrompt.Triggered:Connect(function()
		Triggered(v)
	end)
end

In the for loop at the very bottom, I’m triggering a proximityprompt event, but nothing happens when I trigger it.

image

4 Likes

You’re getting the subprogram from the Triggered function, but you aren’t actually running it.

You could just do:

v.ProximityPrompt.Triggered:Connect(Triggered(v))

or

v.ProximityPrompt.Triggered:Connect(function(player)
    Triggered(v)(player)
end)
4 Likes

I tried the high order function method and it didn’t work but I haven’t tried the 2nd method. Why did you put the player in parentheses on the outside?

I also would like to mention I printed before I called the Triggered function and it still didn’t work.

2 Likes

I put the player as a parameter outside of the other function. This is because when we call Triggered, it returns a function. We then pass the player as a parameter to this function, which is the intended function to be connected to the event. By passing it parameters, we are calling the function and it is running.

If that second method doesn’t work, I think you have over-simplified your code. Try making it easier to read, and don’t use a function to get a connection event.

2 Likes

Yeah the second method didn’t work for me either, I also want to clarify my function was the exact same.

2 Likes

Just to be 100% sure, it’s the bottom iteration that isn’t working?

Currently the code you have for it is less efficient, using an extra function.

Try:

--if you don't want to use pairs() or ipairs()
for i, v in next, Updates:GetChildren(), nil do
    local prompt = v:FindFirstChildOfClass("ProximityPrompt")
    if not prompt then continue end
    prompt.Triggered:Connect(function(player)
        print("@"..player.Name.." triggered the prompt.")
    end)
end
1 Like

I didn’t get anything in the output.

1 Like

Check:

  • Updates:GetChildren() actually returns an array
  • They aren’t all just being skipped with continue

I’ve tested that method of iteration and it works, try others too. Debug with print statements.

You could just use GetDescendants() and check if it’s a prompt or not when iterating.

1 Like

Which container is ClickTools located in? Is it ServerStorage? If it is located in ServerStorage, is GunHandler's RunContext set to Server? By default, Scripts only run when they’re a descendant of workspace or ServerScriptService. Once you confirmed that, what @12345koip gave you here:

should work fine.

2 Likes

ClickTools is in the workspace, I also tried the high order function but it didn’t work, what I have displayed right now is me trying something new.

1 Like

They exist and you can print them out and the proximity prompts, all the instances, but it will never print a statement in the event.

1 Like

I’m not sure but check the properties of the prompt. Might be something wrong with those, if the code is running.

1 Like

Strange. Do you modify Suppression in any other script in the DataModel?

1 Like

Well RequiresLineOfSight is enabled.

1 Like

I haven’t modified the instance anywhere else.

1 Like

Do you have any lines of code in the module that might cause it to yield? If the module yields, the script requiring it yields too.

1 Like

I’m using a normal server script btw and nothing is yielding my code.

No, I mean in the module that you required. Yielding in there causes yielding in this script if it is calling a yielding thread.

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tools = ServerStorage.Tools
local Remotes = ReplicatedStorage.Remotes

local function FindWeaponInCharacter(player)
	local character = player.Character
	for i,v in Tools:GetChildren() do
		local tool = character:FindFirstChild(v.Name)
		if tool then
			return tool
		end
	end
end

local Distributer = {}

function Distributer:Init(player,instance)
	local backpack = player.Backpack
	local name = instance.Name
	local gun = Tools:FindFirstChild(name):Clone()
	gun.Parent = backpack
end

function Distributer:Upgrade(player,instance)
	if instance.Name == "Suppression" then
		self:Suppression(player)
		return
	elseif instance.Name == "FlashHider" then
		return
	
	end
end

function Distributer:Suppression(player)
	local tool = FindWeaponInCharacter(player)
	if tool then
		Remotes.ToClient:FireClient(player,"Suppression",tool)
	end
end

return Distributer

This is all my module has.

I’d also like to mention this script worked for me before its just not working for me now.

AI assistant said its a client-side event -