Touched function not working consistently

  1. What do you want to achieve?
    I am still somewhat new to LUA and I am trying to make it so when the brick that follows the mouse touches a ghost it always deals damage and not when you’re just up close or touch a specific spot.

  2. What is the issue?
    It is very inconsistent where sometimes it starts damaging the ghost right off the bat, and then other times it just damages only in a specific spot or not at all until you get up close and even then it is very inconsistent.

  3. What solutions have you tried so far?
    I have been looking around the developer hub and I tried one thing with “GetTouchedParts” which didn’t seem to work on my case and I have heard some others thrown around such as Region3 and Raycasting but I was hoping to get a second opinion on what the problem could be.

Server Script in the Tool

local Players = game:GetService("Players")
local Tool = script.Parent.Parent
local Stream = script.Parent.Tip.Beam
local Tip2 = script.Parent:WaitForChild("Tip2")
local Barrel = script.Parent.Barrel
local Flash = script.Parent.Tip.ParticleEmitter
local MovingAtt = Tip2:WaitForChild("movingattachment")
local RunService = game:GetService("RunService")
local Fired = script.Parent.Parent.Fired
local CaptureStream = script.Parent.Parent.CaptureStream
local Released = script.Parent.Parent.Released
local AlignPositionInstance = Instance.new("AlignPosition")
local Debounce = false
local Debounce2 = false
local Ammunition = 0
-----------------------------------------------------------
Fired.OnServerEvent:Connect(function(Player,Mouse,MousePosition, Touched)
	Stream.BeamLightning.Enabled = true
	Stream.BeamCore.Enabled = true
	Stream.BeamGlow.Enabled = true
	Tip2.CanTouch = true
	Tip2.CFrame = CFrame.new(MousePosition)
	Flash.Enabled = true
	Barrel.Fire.Playing = true
	Barrel.StopFiring.Playing = false
	Barrel.StopFiring.TimePosition = 0
	if Touched ~= nil then
		local GhostHumanoid = Touched.Parent:FindFirstChild("GhostHumanoid")
		if Debounce2 == false and GhostHumanoid.Health >= 25 then
			Debounce2 = true
			GhostHumanoid.Health = GhostHumanoid.Health - 5
			wait(1)
			Debounce2 = false
		end
	end
end)
-----------------------------------------------------------
Released.OnServerEvent:Connect(function(Player, Touched)
	Stream.BeamLightning.Enabled = false
	Stream.BeamCore.Enabled = false
	Stream.BeamGlow.Enabled = false
	Barrel.Fire.Playing = false
	Barrel.StopFiring.Playing = true
	Barrel.Fire.TimePosition = 0
	Flash.Enabled = false
	Debounce2 = false
	if Touched ~= nil then
		local Torso = Touched.Parent:FindFirstChild("Torso")
		local AlignPosition = Torso:FindFirstChild("AlignPosition")
		local CaptureEffect = Torso:FindFirstChild("Captured")
		if AlignPosition ~= nil then
			Torso:SetNetworkOwner(nil)
			AlignPosition.Parent = game.ServerStorage
			Stream.BeamLightning.Attachment1 = MovingAtt
			Stream.BeamCore.Attachment1 = MovingAtt
			Stream.BeamGlow.Attachment1 = MovingAtt
			CaptureEffect.Enabled = false
			Flash.Enabled = false
		end
	end
end)
-----------------------------------------------------------
CaptureStream.OnServerEvent:Connect(function(Player, MousePosition, Touched)
	local Torso = Touched.Parent:FindFirstChild("Torso")
	local TorsoAttachment = Torso:FindFirstChild("Torso")
	local CaptureEffect = Torso:FindFirstChild("Captured")
	local Distance = 25
	if Debounce == false then
		AlignPositionInstance.Mode = Enum.PositionAlignmentMode.OneAttachment
		AlignPositionInstance.RigidityEnabled = true
		AlignPositionInstance.Attachment0 = TorsoAttachment
		AlignPositionInstance.Parent = Torso
		Debounce = true
		local LV = CFrame.new(Tool.Handle.Tip.lasersource.WorldPosition,MousePosition)
		local AlignPosition = Torso:FindFirstChild("AlignPosition")
		AlignPosition.Position = Tool.Handle.Tip.lasersource.WorldPosition + LV.LookVector * Distance
		Torso:SetNetworkOwner(Player)
		Stream.BeamLightning.Attachment1 = TorsoAttachment
		Stream.BeamCore.Attachment1 = TorsoAttachment
		Stream.BeamGlow.Attachment1 = TorsoAttachment
		CaptureEffect.Enabled = true
		wait(1)
		Debounce = false
	end
end)
-----------------------------------------------------------

Rather than a “brick that follows the mouse”, use RunService:BindToRenderStep and in the callback, raycast the LookVector of the gun barrel and use hit.instance. You can use a server-side check to validate the hit and guard against cheats.

For a fast and thin projectile like this Raycast would indeed be better. But if you intend on using the .Touched event, I think the problem is that the actual collision box isn’t the same as what you are seeing on the screen. You might want to either change the CollisionFidelity of your ghost, or add an invisible hitbox part.

A simple server-side check would be to ray-cast from the ghost to the player’s gun and ensure there is nothing blocking in-between.

I’ll be sure to try that when I return home in about 7 hours and I’ll see if that works. Thank you for the advice.

no need to use Raycast please just use Mouse.Hit.Position or Mouse.Target which detects the part or instance where the player clicks Click

Already using that in the local script, that’s what I use for the brick to follow wherever you click.

You got to stop responding to threads you have no expertise in (based on your response to the last posts). I critiqued you one time, and now you’re trying to prove me wrong in every single thread I post on.

Using Mouse.Hit.Position or Mouse.Target is NOT the way to do this kind of work. Imagine a player is behind a wall. The player then zooms their camera out to see behind the wall and clicks on the ghost. How would the gun be able to shoot through the wall directly to hit the ghost? Well to solve this problem, you use a Raycast, which would detect the wall.

That’s right… the game is in the first person… I think

I know Raycasting would be good for something like this but I am wanting to see if I can get the .Touched event working, and if not I will move to Raycasting. While messing with the touched function alone on some bricks, I was wondering if it could be something in the local script as I have yet to put that up since I think the ServerScript functions as it should. I already also messed with the CollisionFidelity of the ghost mesh and it seemed to do nothing.

Here is the local script I was messing around with in a separate brick if you’re interested, I didn’t seem to get it to print anything so I don’t know if I just don’t know how to properly use a touched function, or something is wrong with my studio. If you are interested in seeing the local script in the gun I can edit the post and put it in there.

image

If you put a local script in the workspace it will not run (I think the only exception is the Animate script for the players). If you intend for this script to run on the client, you could create a Script object, set its RunContext to Client, and put your code inside the new script.

The local script is within the brick itself, not the workspace unless I read that wrong and you just meant anywhere in the workspace such as a brick or a model.

Edit: So that worked for the brick and I decided to test it out in the tool model to change a script runcontext to client and threw the original local script code into that and it didn’t work, I assume i’ll probably have to rewrite it. Thank you for taking your time and responding to these questions as I said I am pretty new to LUA.

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