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

I need a part that cancollide = true if it's under 50 speed, and cancollide = false if it's over?

Basically it's designed for a vehicle, if the vehicle is going over 50 SPS, the part detects it and cancollide = false, and back to cancollide = true if it's under. Except this doesn't happen. What have I done wrong?

P = script.Parent
Player = game.Players.LocalPlayer
Human = Player.Character
Torso = Human.Torso


while true do
    wait(.05)
    local intSpeed = math.floor((Torso.Velocity.magnitude)/1.6)
    if Torso.Velocity >= 50  then P.CanCollide = false end
    if Torso.Velocity <= 49 then P.CanCollide = true end
end

1 answer

✓ Accepted answer

It looks like you may be mixing some server and client components of scripting. If the Part is the parent of the script then it would likely be a server Script; however, the LocalPlayer can only be used from a client LocalScript.

I would suggest to make a LocalScript and put it into StarterPlayerScripts or StarterGui. Next I'll make a few modifications to your code to put in the LocalScript.

local P = game.Workspace.Part --change this to where the Part is located if this is not it
local Player = game.Players.LocalPlayer
local Torso = (Player.Character or Player.CharacterAdded:wait()):WaitForChild("Torso") -- waits until the character first spawns, yes it's a little complicated

Player.CharacterAdded:connect(function(Character) -- gets the new Torso if the character respawns
	Torso = Character:WaitForChild("Torso")
end)

while true do
	wait(0.05)
	if Torso.Velocity.magnitude >= 50 then -- an if else statement is better to use than two if statements in this case
		P.CanCollide = false
		P.Transparency = 0.5
	else
		P.CanCollide = true
		P.Transparency = 0
	end
end

2 comments

0

Thanks, works great. Although, it doesn't work when you use :insert [id] with admin commands. That doesn't really matter to me, but if there's a way round that i'd really appreciate it.

0

I made a script inside the brick to instantly move the LocalScript to StarterGui. However, it only works when not inserted via admin commands.

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