.Touched doesn't detect other characters

So I have the following script (server):

script.Parent.Player:GetPropertyChangedSignal("Value"):Connect(function()
	if script.Parent.Player.Value:IsA("Player") then
		game:GetService("RunService").Stepped:Connect(function()
			script.Parent.CFrame = script.Parent.Player.Value.Character.HumanoidRootPart.CFrame
		end)
	end
end)

script.Parent.Touched:Connect(function(part)
	print(part)
	if not(part.Parent == script.Parent.Player.Value.Character) then
		if part.Parent:FindFirstChildOfClass("Humanoid") then
			part.Parent:FindFirstChildOfClass("Humanoid").Health = 0
		end
	end
end)

Issue:

No references of other characters are made. Help?

Wouldn’t that be performance intensive?

please do not forget

Use WorldRoot:GetPartsInParts

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/GetPartsInPart

localscript, StarterCharacterScripts

local Part = Instance.new("Part")
Part.Size = Vector3.new(3, 6.5, 3)
Part.Anchored = true
Part.Transparency = 0.5
Part.CanCollide = false
Part.Parent = workspace

game:GetService("RunService").Stepped:Connect(function()
	Part.CFrame = script.Parent.HumanoidRootPart.CFrame

	local OverlapParameters = OverlapParams.new()
	OverlapParameters.MaxParts = math.huge
	OverlapParameters.FilterDescendantsInstances = script.Parent:GetDescendants()
	
	local PartsInPart = workspace:GetPartsInPart(Part, OverlapParameters)

	for i, part in pairs(PartsInPart) do
		if part.Parent:FindFirstChildOfClass("Humanoid") then
			part.Parent:FindFirstChildOfClass("Humanoid").Health = 0
		end
	end
end)

RunService.RenderStepped does not work on serverscripts

1 Like

I know this is solved but for future reference use the ‘Changed’ event/signal when using value objects.

local Parent = script.Parent
local Player = Parent.Player

local function OnChanged(Value)
	if Value:IsA("Player") then
		--Do code.
	end
end

Player.Changed:Connect(OnChanged)

Doesn’t really matter, as they both do the same thing. With values with changing parents, I would use GetPropertyChangedSignal().

No they don’t, the ‘Changed’ event/signal behaves differently for value object instances.
https://developer.roblox.com/en-us/api-reference/event/IntValue/Changed
It only fires when the ‘Value’ property changes and it passes the new value of its ‘Value’ property to any connected callback functions.

Good remark, never noticed that. So that means I should use GetPropertyChangedSignal if I want to listen for Name/Parent, right?

So that means I should use GetPropertyChangedSignal if I want to listen for Name/Parent, right?

Yes and ‘Archivable’ (if necessary).

1 Like