Touched/TouchEnded Event not firing on sphere part

This is the code which is inside a Localscript, the event doesn’t seem to be firing correctly/properly, on range, which is a sphere part and when the humanoid goes in and out it doesn’t seem to fire.

local broom = script.Parent
local range = broom:WaitForChild('Range')
local player = game:GetService('Players').LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild('Humanoid')
local userInputService = game:GetService('UserInputService')
local replicatedStorage = game:GetService('ReplicatedStorage')
local unequipBroom = replicatedStorage.Broom.UnequipBroom
local rideBroom = replicatedStorage.Broom.RideBroom
local leaveBroom = replicatedStorage.Broom.LeaveBroom
local equipBroom = replicatedStorage.Broom.EquipBroom
local remoteFunction = replicatedStorage.Broom.RemoteFunction
local broomEquipped = true
local connection
local connection2

broom.Equipped:Connect(function()
	connection = userInputService.InputBegan:Connect(function(input, gameProcessed)
		if gameProcessed then return end

		if input.KeyCode == Enum.KeyCode.E then
			broomEquipped = false
			unequipBroom:InvokeServer(broom)
		elseif input.KeyCode == Enum.KeyCode.F then
			broomEquipped = false
			unequipBroom:InvokeServer(broom)
			rideBroom:FireServer(broom)
		end
	end)
end)

broom.Unequipped:Connect(function()
	if connection then
		connection:Disconnect()
	end
end)

range.Touched:Connect(function(hit)
	print(hit, hit.Parent)
	if not hit.Parent:FindFirstChild('Humanoid') then return end
	
	print(hit.Parent)
	--if broomEquipped then return end

	connection2 = userInputService.InputBegan:Connect(function(input, gameProcessed)
		if gameProcessed then return end

		print(input.KeyCode)

		if input.KeyCode == Enum.KeyCode.E then
			equipBroom:InvokeServer(range.Parent)
		elseif input.KeyCode == Enum.KeyCode.F then
			leaveBroom:InvokeServer(range.Parent)
			equipBroom:InvokeServer(range.Parent)
		end
	end)
end)

range.TouchEnded:Connect(function(hit)
	if not hit.Parent:FindFirstChild('Humanoid') then return end
	--if player.Character ~= hit.Parent then return end
	--if hit == '' then return end

	if connection2 then
		connection2:Disconnect()
	end
end)```
3 Likes

Wouldn’t it just be easier to use ProximityPrompts?

1 Like

Ah, I never knew I could do this, I’ll have a look at this thank you

1 Like

Just noted couple of problems here but with your range.Touched connection, every time anyone touches it, it will create that new connection because you don’t actually check which player touches it. Also, you don’t need this many inputbegan connections. You can create a variable that shows if the tool is equipped and you can also use :GetTouchingParts() instead of a touch event and put everything within a InputBegan connection. I hope this wasn’t too confusing ik this is like the most confusing thing you’ve ever read but if you have any questions just reply I guess.

1 Like

Thank you, I will use this to improve my code.

1 Like

My ProximityPrompt doesn’t seem to be triggering

local broom = script.Parent
local range = broom:WaitForChild('Range')
local player = game:GetService('Players').LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild('Humanoid')
local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteFunction = replicatedStorage.Broom.RemoteFunction
local broomEquipped = true

local unequipPrompt = broom.Head.UnequipPrompt
local equipPrompt = broom.Shaft.EquipPrompt
local ridePrompt = broom.Shaft.RidePrompt
local leavePrompt = broom.Shaft.LeavePrompt
local unequipBroom = replicatedStorage.Broom.UnequipBroom
local rideBroom = replicatedStorage.Broom.RideBroom
local leaveBroom = replicatedStorage.Broom.LeaveBroom
local equipBroom = replicatedStorage.Broom.EquipBroom

unequipPrompt.Triggered:Connect(function(player)
	print('hi', player)
	unequipPrompt.Enabled = false
	unequipBroom:InvokeServer(broom)
end)

equipPrompt.Triggered:Connect(function(player)
	equipPrompt.Enabled = false
	unequipPrompt.Enabled = true
	equipBroom:InvokeServer(range.Parent)
end)

ridePrompt.Triggered:Connect(function(player)
	rideBroom:FireServer(broom)
end)

leaveBroom.Triggered:Connect(function(player)
	leaveBroom:FireServer(broom)
end)

your print might be bugging your code I think. Try:
print("hi" .. player.Name")

Btw where is this code located? Are there any errors?

image
It’s a local script, the print doesn’t seem to be the problem.

Any errors though? I think the code is loading before the proximity prompts load in, so the reference is dead.

No errors. It shows the ProximityPrompt in game I can press the key E but nothing happens after. I’ll try with :WaitForChild.
Update: Still doesn’t seem to work.

Can you put a print within all of the connections? It might be a problem with your remotes.

It seems I deleted an unneeded part when changing to ProximityPrompts and forgot to remove the line where it :WaitForChild('Range'), apologies.

1 Like

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