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.
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.
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.
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
Which container is ClickTools located in? Is it ServerStorage? If it is located in ServerStorage, is GunHandler'sRunContext 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:
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