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

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

Asked by
cc0de 35
8 years ago

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

Log in to vote
0
Answered by 8 years ago

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
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. cc0de 35 — 8y
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. cc0de 35 — 8y
Ad

Answer this question