How do I stop 2 scripts from interacting with other scripts with bindable events

so I’ve got a pump that, when finishing a proximity prompt, should collect the liquid from that pump. Problem is, I’m using Bindable Events to do this because I have to communicate between two server sided scripts, yet when I do this, every other pump picks up the fired Bindable event.

How do I stop it from happening? And is there any alternative?

Hirearchy:
Screenshot_1

Prompt script:

--{{ Variable Locations }}--
local BE 			  = game.ReplicatedStorage:WaitForChild("BindableEvent")
local Collect 		  = game.ReplicatedStorage:WaitForChild("CollectEvent")
local stopanim 		  = game.ReplicatedStorage:WaitForChild("StopAnimGlobal")
local ProximityPrompt = script.Parent
local Valve 		  = script.Parent.Parent
local Root 			  = script.Parent.Parent.Root

--{{ Debounces }}--
local playerHolding -- primary debounce

--{{ Numeric Variables }}--
local refillTime 	  = 5 -- How much it takes to refill it
local aetherValue 	  = 100 -- How much Aether can it store

--{{ Animation }}--
local animation 	  = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9396489358"

--{{ COLLECT (1) & Prompt Debounce}}
ProximityPrompt.Triggered:Connect(function(player)
	ProximityPrompt.Enabled = false
	BE:Fire(player)
	print("PromptTriggered "..player.leaderstats.Aether.Value)
	wait(3)
	ProximityPrompt.Enabled = true
end)

--{{ While Holding }}--
function Hold(player)
	local HRP 	    = player.Character:WaitForChild("HumanoidRootPart")
	local hum 	    = player.Character:WaitForChild("Humanoid")
	local lookatp   = script.Parent.Parent.LookAtPart
	HRP.CFrame 	    = CFrame.lookAt(HRP.Position, Vector3.new(lookatp.Position.X, lookatp.Position.Y, lookatp.Position.Z))

	local valveAnim = hum.Animator:LoadAnimation(animation)
	
	if playerHolding then return end
	playerHolding   = player
	valveAnim:Play()
	wait()
	
	HRP.CFrame      = CFrame.lookAt(HRP.Position, Vector3.new(lookatp.Position.X, lookatp.Position.Y, lookatp.Position.Z))
	hum.AutoRotate  = false
	HRP.Position    = Root.Position
	HRP.Anchored    = true
	
	stopanim.Event:Connect(function(player)
		valveAnim:Stop()
	end)
	
	repeat
		Valve.CFrame = Valve.CFrame*CFrame.Angles(-0.1, 0, 0)
		task.wait(1/40)
	until not playerHolding 
end

--{{ When Stopped Holding }}
function HoldEnded(player)
	local HRP 		  = player.Character:WaitForChild("HumanoidRootPart")
	local hum 		  = player.Character:WaitForChild("Humanoid")
	if playerHolding == player then
		playerHolding = nil
	end
	stopanim:Fire()
	
	hum.AutoRotate 	  = true
	HRP.Anchored   	  = false
end


--{{ COLLECT (2) }}
Collect.Event:Connect(function(player)
	player.leaderstats.Aether.Value = player.leaderstats.Aether.Value + aetherValue
	print("CollectEvent "..player.leaderstats.Aether.Value)
end)

ProximityPrompt.PromptButtonHoldBegan:Connect(Hold)
ProximityPrompt.PromptButtonHoldEnded:Connect(HoldEnded)

Liquid Script

--{{ Variable Locations }}--
local BE 			  = game.ReplicatedStorage:WaitForChild("BindableEvent")
local Collect 		  = game.ReplicatedStorage:WaitForChild("CollectEvent")
local stopanim 		  = game.ReplicatedStorage:WaitForChild("StopAnimGlobal")
local ProximityPrompt = script.Parent
local Valve 		  = script.Parent.Parent
local Root 			  = script.Parent.Parent.Root

--{{ Debounces }}--
local playerHolding -- primary debounce

--{{ Numeric Variables }}--
local refillTime 	  = 5 -- How much it takes to refill it
local aetherValue 	  = 100 -- How much Aether can it store

--{{ Animation }}--
local animation 	  = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9396489358"

--{{ COLLECT (1) & Prompt Debounce}}
ProximityPrompt.Triggered:Connect(function(player)
	ProximityPrompt.Enabled = false
	BE:Fire(player)
	print("PromptTriggered "..player.leaderstats.Aether.Value)
	wait(3)
	ProximityPrompt.Enabled = true
end)

--{{ While Holding }}--
function Hold(player)
	local HRP 	    = player.Character:WaitForChild("HumanoidRootPart")
	local hum 	    = player.Character:WaitForChild("Humanoid")
	local lookatp   = script.Parent.Parent.LookAtPart
	HRP.CFrame 	    = CFrame.lookAt(HRP.Position, Vector3.new(lookatp.Position.X, lookatp.Position.Y, lookatp.Position.Z))

	local valveAnim = hum.Animator:LoadAnimation(animation)
	
	if playerHolding then return end
	playerHolding   = player
	valveAnim:Play()
	wait()
	
	HRP.CFrame      = CFrame.lookAt(HRP.Position, Vector3.new(lookatp.Position.X, lookatp.Position.Y, lookatp.Position.Z))
	hum.AutoRotate  = false
	HRP.Position    = Root.Position
	HRP.Anchored    = true
	
	stopanim.Event:Connect(function(player)
		valveAnim:Stop()
	end)
	
	repeat
		Valve.CFrame = Valve.CFrame*CFrame.Angles(-0.1, 0, 0)
		task.wait(1/40)
	until not playerHolding 
end

--{{ When Stopped Holding }}
function HoldEnded(player)
	local HRP 		  = player.Character:WaitForChild("HumanoidRootPart")
	local hum 		  = player.Character:WaitForChild("Humanoid")
	if playerHolding == player then
		playerHolding = nil
	end
	stopanim:Fire()
	
	hum.AutoRotate 	  = true
	HRP.Anchored   	  = false
end


--{{ COLLECT (2) }}
Collect.Event:Connect(function(player)
	player.leaderstats.Aether.Value = player.leaderstats.Aether.Value + aetherValue
	print("CollectEvent "..player.leaderstats.Aether.Value)
end)

ProximityPrompt.PromptButtonHoldBegan:Connect(Hold)
ProximityPrompt.PromptButtonHoldEnded:Connect(HoldEnded)

btw no, I cannot just make another event, I will have a placement system and multiple will be placed.

1 Like

Bump?

In short, is there a way to fire a bindable event to a particular script instead of all scripts which connect?

I’m not sure if this is efficient or not but you can pass a random value "Egg" for example and make the undesired scripts to check if the the value is "Egg"

Example:

--In the pipe script
BindableEvent.Event:Connect(function(Check -- this is where we store the egg value)
     if not Check then -- this will only fire when Check is nil
     -- your code here
     end
end)

again I’m not sure if this is a good approach but you can try it anyway

1 Like

how will I diferentiate the desired script from the undesired script?

The correct way would to put the bindable event inside the Aether Pump that way when you clone the Aether Pump it will also clone a new bindable event

the ugle way would be

local pump = script.Parent.Parent.Parent
BE:Fire(player, pump)
local pump = script.Parent.Parent.Parent
BE.Event:Connect(function(player, eventPump)
        -- if the pump is not the same then exit
        if pump ~= eventPump then return end
        -- code
end)

now the player doesn’t collect the liquid, nor does it add value in the leaderstats, maybe I implemented the code wrong
(also, Pump is capitalized both in event connect and in ‘local Pump’)
Liquid Script

local Pump = script.Parent.Parent.Parent
-- stuff
BE.Event:Connect(function(player, eventPump)
	if Pump ~= eventPump then return end
	if aether.Size == Vector3.new(10, 4.7, 4.7) then
		if TweenUp.Completed then
			TweenDown:Play()
			Collect:Fire(player)
		end
	end
end)

Prompt Script

local Pump = script.Parent.Parent.Parent
-- stuff
ProximityPrompt.Triggered:Connect(function(player)
	ProximityPrompt.Enabled = false
	BE:Fire(player, Pump)
	wait(3)
	ProximityPrompt.Enabled = true
end)
  • Output gives no errors

make sure you set the correct amount of parents i think the Liquid only has to go up 2 parents to get to the pump model and the prompt has to go up 4 parents to get the pump model

-- Prompt Script
local pump = script.Parent.Parent.Parent.Parent
-- Liquid Script
local pump = script.Parent.Parent

make sure the pump is the same value on both scripts

While this did fix the ‘not collecting’ issue, the pumps still work as if both valves were turned instead of one.
Prompt

--{{ Variable Locations }}--
local BE 			  = game.ReplicatedStorage.Signals.Bindables.Events:WaitForChild("BindableEvent")
local Collect 		  = game.ReplicatedStorage.Signals.Bindables.Events:WaitForChild("CollectEvent")
local stopanim 		  = game.ReplicatedStorage.Signals.Bindables.Events:WaitForChild("StopAnimGlobal")
local ProximityPrompt = script.Parent
local Valve 		  = script.Parent.Parent
local Pump            = script.Parent.Parent.Parent.Parent
local Root 			  = script.Parent.Parent.Root

--{{ Debounces }}--
local playerHolding -- primary debounce

--{{ Numeric Variables }}--
local refillTime 	  = 5 -- How much it takes to refill it
local aetherValue 	  = 10 -- How much Aether can it store

--{{ Animation }}--
local animation 	  = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9396489358"

--{{ COLLECT (1) & Prompt Debounce}}
ProximityPrompt.Triggered:Connect(function(player)
	ProximityPrompt.Enabled = false
	BE:Fire(player, Pump)
	wait(3)
	ProximityPrompt.Enabled = true
end)

--{{ While Holding }}--
function Hold(player)
	local HRP 	    = player.Character:WaitForChild("HumanoidRootPart")
	local hum 	    = player.Character:WaitForChild("Humanoid")
	local lookatp   = script.Parent.Parent.LookAtPart
	HRP.CFrame 	    = CFrame.lookAt(HRP.Position, Vector3.new(lookatp.Position.X, lookatp.Position.Y, lookatp.Position.Z))

	local valveAnim = hum.Animator:LoadAnimation(animation)
	
	if playerHolding then return end
	playerHolding   = player
	valveAnim:Play()
	wait()
	
	HRP.CFrame      = CFrame.lookAt(HRP.Position, Vector3.new(lookatp.Position.X, lookatp.Position.Y, lookatp.Position.Z))
	hum.AutoRotate  = false
	HRP.Position    = Root.Position
	HRP.Anchored    = true
	
	stopanim.Event:Connect(function(player)
		valveAnim:Stop()
	end)
	
	repeat
		Valve.CFrame = Valve.CFrame*CFrame.Angles(-0.1, 0, 0)
		task.wait(1/40)
	until not playerHolding 
end

--{{ When Stopped Holding }}
function HoldEnded(player)
	local HRP 		  = player.Character:WaitForChild("HumanoidRootPart")
	local hum 		  = player.Character:WaitForChild("Humanoid")
	if playerHolding == player then
		playerHolding = nil
	end
	stopanim:Fire()
	
	hum.AutoRotate 	  = true
	HRP.Anchored   	  = false
end


--{{ COLLECT (2) }}
Collect.Event:Connect(function(player)
	player.leaderstats.Aether.Value += aetherValue
end)

ProximityPrompt.PromptButtonHoldBegan:Connect(Hold)
ProximityPrompt.PromptButtonHoldEnded:Connect(HoldEnded)

Liquid

--{{ Services & Variable Locations }}--
local TweenService = game:GetService("TweenService")

local BE 		   = game.ReplicatedStorage.Signals.Bindables.Events:WaitForChild("BindableEvent")
local Collect 	   = game.ReplicatedStorage.Signals.Bindables.Events:WaitForChild("CollectEvent")
local aether 	   = script.Parent
local Pump         = script.Parent.Parent
local valve 	   = script.Parent.Parent.Valve.ValveHandle

--{{ Numeric Variables }}
local refillTime   = 5 -- How much it takes to refill it

--{{ Tweening }}--
local TweenDown    = TweenService:Create(aether, TweenInfo.new(1), {Size = Vector3.new(0.1, 4.7, 4.7)})
local TweenUp      = TweenService:Create(aether, TweenInfo.new(refillTime), {Size = Vector3.new(10, 4.7, 4.7)})

--/ Main gameloop
TweenDown:Play()
repeat wait() until TweenDown.Completed
TweenUp:Play()
repeat wait() until TweenUp.Completed

BE.Event:Connect(function(player, eventPump)
	if Pump ~= eventPump then return end
	if aether.Size == Vector3.new(10, 4.7, 4.7) then
		if TweenUp.Completed then
			TweenDown:Play()
			Collect:Fire(player)
		end
	end
end)

TweenDown.Completed:Connect(function()
	wait(5)
	TweenUp:Play()
end)

I have 2 different models, doesn’t matter if they have the same name or a different one.

I’m not to sure what’s going on but I’m guessing you have to do the same for all the bindable events

Today, I rejoined Studio and looked a bit at my pumps. All of a sudden they worked! Even if only one pump was emptying, it would show as both pumps were collected and give me x2 the amount I should’ve gotten.
But, because I did the Pump and eventPump to the collect event that seemed to fix it aswell, thank you!