Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with touched event?

For some reason, the touched event isn't working. I don't know why! Help!

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local Handle = tool:WaitForChild("Handle")
local debounce = true
local m = player:GetMouse()
local speed = 150




				
tool.Activated:connect(function()
if debounce == true then
debounce = false
local sound = tool.Sound
sound:Play()
wait(.3)
sound:Stop()
local pos = tool.Handle;
local endPos = m.Hit.p;					
local ball = Instance.new("Part", workspace)	
ball.Name = "Ball_" .. player.Name;
ball.CanCollide = false;
ball.Shape = "Ball";
ball.Size = Vector3.new(1, 1, 1);		
local ray = Ray.new(pos.CFrame.p, (endPos - pos.CFrame.p).unit * 300); 		
local hit, position = workspace:FindPartOnRay(ray, player.Character);
local distance = (position - pos.CFrame.p).magnitude;		
ball.CFrame = CFrame.new(position, pos.CFrame .p) * CFrame.new(0, 0, 1.5) * CFrame.new(0, 0, -distance);		
game.Debris:AddItem(ball, 1);		
local sT = 0;			
local bv = Instance.new("BodyVelocity", ball);		
bv.velocity = -ball.CFrame.lookVector * speed;
wait(.4)
debounce = true
ball.Touched:connect(function(hit)
	if hit.Parent.Torso:FindFirstChild("Fire") then
		local fire = hit.Parent.Torso.Fire
		game:GetService("Debris"):AddItem(fire, .01)
	end
end)
end
end)

3 comments

0

does everything else work? Try to remove the if statement and see if it works, if it does then you know where the problem is located, then you can just work around that. I can't see atm why it isn't working, maybe because I am pretty tired.

0

Now it says this...

0

13:59:41.042 - Torso is not a valid member of Workspace

2 answers

✓ Accepted answer
ball.Touched:connect(function(hit)
	--Check to make sure what you hit still exists and if a Torso exists.
	if ((hit ~= nil) and (hit.Parent:findFirstChild("Torso"))) then
		if (hit.Parent.Torso:FindFirstChild("Fire")) then -- Check for fire.
			-- Remove the fire
			game:GetService("Debris"):AddItem(hit.Parent.Torso.Fire, .01)
		end
	end
end)

Also, move the ball.Touched function after

ball.Size = Vector3.new(1, 1, 1); 

So the touched event isn't created 0.4 seconds after the ball is created...

This discussion is preserved as it was. New answers, comments, and votes are closed.