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

Why does this script not detect collisions correctly?

This script is inside a brick that is one stud wide and a vehicle travelling at 60sps is going through that brick. This is meant to change the value in the vehicle yet it only does it some of the time, usually when going slower. (Rarely works on the first collision either)

I've tried making the brick longer but no luck. Can anyone see why?

Detector = script.Parent
enabled = true
BrickValue = 31

function onTouch(part)
	if not enabled then return end 
		enabled = false

		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		local human = part.Parent:findFirstChild("Humanoid") 
		if (human ~= nil) and player~= nil then 

			local bus = Workspace:FindFirstChild(player.Name.."'s Car")
			if bus:FindFirstChild("Fifteen")then
			bus.CurrentScore.Value = bus.CurrentScore.Value + BrickValue
			wait(3)				 
						
		end	
		end
	wait()
	enabled = true 
end


Detector.Touched:connect(onTouch)

2 comments

0

Collision detection are not very good at high speeds. You could always try a Ray Cast.

0

I could help because I did something similar to this.If you would like?

2 answers

You checked the player before the time.

Detector = script.Parent
enabled = true
BrickValue = 31

function onTouch(part)
    if not enabled then return end 
        enabled = false
        if part.Parent:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(part.Parent) then 
            local bus = Workspace:FindFirstChild(player.Name.."'s Car")
            if bus:FindFirstChild("Fifteen")then
            bus.CurrentScore.Value = bus.CurrentScore.Value + BrickValue
            wait(3)              

        end 
        end
    wait()
    enabled = true 
end


Detector.Touched:connect(onTouch)

But don't forget to look for an if statement that doesn't fit. Put some prints in each part and look the output.

3 comments

0

That is my guess, I'm not sure.

0

It doesn't work at high speeds as hiccup111 said :/ Thanks for trying though :p

0

I don't see why, but ok.

just a few things to say, game.Players:GetPlayerFromCharacter() doesn't work... u need to give the system the Character not the Players to get the PlayerFromCharacter... also, u don't need if (this ~= nil) and anotherthing ~= nil then u don't need those Brackets :P

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