Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with touched event?

Asked by 8 years ago

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)
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. Kryddan 261 — 8y
0
Now it says this... swimmaster07 25 — 8y
0
13:59:41.042 - Torso is not a valid member of Workspace swimmaster07 25 — 8y

2 answers

Log in to vote
0
Answered by
DevSean 270 Moderation Voter
8 years ago
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...

Ad
Log in to vote
0
Answered by 8 years ago

THANK YOU SO MUCH IT WORKS NOW!

Answer this question