Issue detecting touched

You can write your topic however you want, but you need to answer these questions:

  1. I’m trying to make a script where a sphere appears for 0.5 seconds and then gets destroyed, but when that sphere appears, anybody in it’s range, including myself get caught in it and get hit by it, then using a remote event to fire something on all clients hit by the sphere.

  2. For some reason, the “Touched” function isn’t detecting anything, not me nor anybody, I’ve got 0 clue why.

  3. I tried looking for help here on DevForum but got a lot of Raycast hitbox types which isn’t what I’m making this hitbox. I have another hitbox script which worked fine, and the only difference with the code below and the code that works is A: My other code doesn’t detect the user, and B: I create the hitbox part IN the script, while here I am cloning it from ReplicatedStorage.


The code that I am trying to fix that does NOT work:--

        local VoidHitbox = game.ReplicatedStorage.Domain.DomainExpansion.VoidHitbox:Clone()
        VoidHitbox.Parent = game.Workspace
        VoidHitbox.Position = Dome.Position
        local VoidWeld = Instance.new("Weld")
        VoidWeld.Parent = HumanoidRP
        VoidWeld.Part1 = HumanoidRP
        VoidWeld.Part0 = VoidHitbox
        ---------------------------
        VoidHitbox.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                if hit.Name == "HumanoidRootPart" then
                    local TargetHum = hit.Parent:FindFirstChild("Humanoid")
                    local DomainLocalScript = game.ReplicatedStorage.Domain.LocalDomainEvent:Clone()
                    DomainLocalScript.Parent = hit.Parent
                    print(hit.Parent.Name)
                    DomainEvent:FireClient("Expand")
                end
            end
        end)



The code that DOES work:--

local Character = Player.Character or Player.CharacterAdded:Wait()
		local Hitbox = Instance.new("Part", Character)
		local Weld = Instance.new("Weld")
		Weld.Parent = Character.HumanoidRootPart
		Weld.Part0 = Hitbox
		Weld	.Part1 = Character.HumanoidRootPart
		Weld.C0 = CFrame.new(0,-5,18)
		Hitbox.Parent = Character
		Hitbox.Size = Vector3.new(26,22,35)
		Hitbox.Anchored = false
		Hitbox.CanCollide = false
		Hitbox.Massless = true
		Hitbox.Transparency = 1
		Hitbox.Color = Color3.new(1, 0, 0)
		Hitbox.Material = "ForceField"
		
		Hitbox.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				if hit.Name == "HumanoidRootPart" then
					if hit.Parent.Name ~= Character.Name then
						local TargetHum = hit.Parent:FindFirstChild("Humanoid")

If anybody knows what I did wrong or knows how I can improve, please let me know. I’m doing this for practice and to improve but I’ve hit a brick wall.

2 Likes

Also, excuse the messy format, this is my first time posting on DevForum myself, hence the default line being left in.

2 Likes

Is CanTouch and CanQuery set to true on the sphere …

3 Likes

Yes, I have CanCollide set to false, but CanTouch and CanQuery are set to True

1 Like

I assume that DomainEvent is a RemoteEvent? The syntax for FireClient() is as follows:
RemoteEvent:FireClient(player : Player, ... : any?)

You must pass a Player object to FireClient(), and getting the player can be achieved by using Players:GetPlayerFromCharacter():

local PLAYERS = game:GetService("Players")

local playerCharacter = workspace.pretendThisIsAPlayerCharacter

print(PLAYERS:GetPlayerFromCharacter(playerCharacter))
2 Likes

For your use case, you may prefer to make use of workspace:GetPartBoundsInRadius().

I believe that this should be much simpler than having to create a sphere each time, and you won’t have to worry about weird .Touched things.

2 Likes

Correct, DomainEvent is a remote event that I’m firing using a tool that’s firing to server. I see what you mean regarding the FireClient, I’ll have to adjust that too. I’m just unsure why my part isn’t being touched in the first place.

1 Like

I’ve never heard of that before, I’ll have to research that and try it out.

1 Like

I’ll look into “WorldRoot | Documentation - Roblox Creator Hub” and if that yields any results I’ll come back and let you guys know, thank you all for helping me out

1 Like

(that was meant to be workspace:GetPartBoundsInRadius() i just don’t know how to do that.

1 Like

You can try using .Magnitude for this:

game:GetService("RunService").PreRender:Connect(function()

if ("Sphere" - "HumanoidRootPart").Magnitude <= 1 then

-- Fire Event

end

end)
2 Likes

I’ve heard of magnitude before just never understood it, I guess I have that to look into as well. I’m relatively new to scripting so a lot of things still don’t make full sense to me like what to do and how to do it better and vice versa, so thank you guys for helping me.

1 Like

Magnitude is basically the distance from the origin to a given vector (vector could represent a velocity, position, etc.)

2 Likes

Oh I see so I set a position and then whatever I make the magnitude, it will basically just detect anything within said radius around my chosen point?

2 Likes

Not quite. Magnitude just returns a number.

I know what KingBlueDash was trying to get to, however his implementation was flawed. I would recommend just sticking to :GetPartsBoundInRadius()

3 Likes

For hitboxes like this I highly recommend using the ZonePlus module by ForeverHD.
From my experience it’s super accurate and has built-in functions for detecting either players (on the server) or localplayer (on the client).

2 Likes

I’ll 100% try that, I’m still new to modules so I don’t really know how they work or even really how to call them. Things like the lightning modules that people use.

1 Like

I imagine that’s like the other function called like “:GetsPartsInPart”?

1 Like

Pretty similar yeah. Only difference is that the PartsBoundInRadius one does a spherical area while teh PartsInPart one does a box area (the part that is passed as a parameter to the parts in part function.)

2 Likes

For something like this you would just insert the module in ReplicatedStorage, call it using
Zone = require(ReplicatedStorage:WaitForChild(“Zone”)
and to create the “Zone” (hitbox) do
VoidZone = Zone.new(VoidHitbox)
and replace VoidHitBox.Touched with VoidZone.playerEntered which also returns the player who touched it

From my experience it’s best to avoid touched events wherever possible because they are unreliable/inaccurate. You won’t regret taking the leap to ZonePlus :smiley:

2 Likes