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

How do I fix this if & then statement, and please describe to me what I did wrong?

Working on an active train with Velocity's, but I want the train to stop when a player touches the track. I don't want the player to go with the train, when he touches the tracks. Can someone help me fix this script, and tell me what I did wrong?

TA = script.Parent.Parent.Train.A -- Locamotive
TB = script.Parent.Parent.Train.B -- Locamotive
B = script.Parent.Parent.Part  -- Track
player = game.Players
B.Velocity = Vector3.new(-30,0,0) --Train speed


function onTouch()
	if player (onTouch)then
			B.CanCollide = false
			TA.Anchored = true
			TB.Anchored = true
	else
			B.CanCollide = (true)
			TA.Anchored = false
			TB.Anchored = false
			end
	
	end

B.Touched:connect(onTouch)

1 answer

Touched events have a built in parameter, being the part that touched it.

TA = script.Parent.Parent.Train.A -- Locamotive
TB = script.Parent.Parent.Train.B -- Locamotive
B = script.Parent.Parent.Part  -- Track
player = game.Players
B.Velocity = Vector3.new(-30,0,0) --Train speed


function onTouch(hit) -- hit is whatever touched the part.
    if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then -- Check if the parent of whatever hit is a player
            B.CanCollide = false
            TA.Anchored = true
            TB.Anchored = true
    else
            B.CanCollide = (true)
            TA.Anchored = false
            TB.Anchored = false
            end

    end

B.Touched:connect(onTouch)

2 comments

0

One issue, my mistake, but I would like the train to continue if the player comes back off it

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