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

touched event running on play? (part)

so i have this problem where the .Touched event runs as soon as i press play, instead of waiting for the ball to touch it first. here's my script: (ignore the game.Workspace... twice, i had a bug where if i made it local something it would just affect the player instead)

local part = script.Parent

part.Touched:Connect(function(hit)
	local ball = hit.Parent:FindFirstChild("Ball")
	if ball then
		game.Workspace.Ball.Anchored = true
		game.Workspace.Ball.BrickColor = BrickColor.new("Gold")
	end
end)

3 comments

0

Is this the whole script. If yes, what is the part and where is it located? Also, is the Ball in workspace and the ball is a sphere or a model?

0

the part is just a basic part, located in the workspace.

0

the ball is a sphere in the workspace too

1 answer

✓ Accepted answer

Well, according to your code, if any basic part or meshpart [which is not inside a model and in workspace], will trigger your .Touched event.

In order to fix, try the following code:

local part = script.Parent

part.Touched:Connect(function(hit)
    local ball = hit.Name -- Gets the name of the hitting part
    if ball == "Ball" then -- Checks if the name is Ball
        game.Workspace.Ball.Anchored = true
        game.Workspace.Ball.BrickColor = BrickColor.new("Gold")
    end
end)

Lemme know if it helps!

1 comment

0

works perfectly, thank you!

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