Hello. I am trying to make a script that gives a player money while they are driving! This is what I have tried
local cashadd = 335 local distadd = 33 local owner = script.Parent.Parent.Parent.Parent.Parent.Name local user = game.Players:findFirstChild(owner) local data = user:findFirstChild("Playerdata") local stats = user:findFirstChild("leaderstats") ---------- Vector Calculation ---------- function GetSpeed(part) pPos = part.Position; wait(1) oPos = part.Position; pRspeed = {x=0,y=0,z=0} oRspeed = {x=0,y=0,z=0} pRspeed.x = math.floor(pPos.x); pRspeed.y = math.floor(pPos.y); pRspeed.z = math.floor(pPos.z); oRspeed.x = math.floor(oPos.x); oRspeed.y = math.floor(oPos.y); oRspeed.z = math.floor(oPos.z); IForce = {x=0,y=0,z=0} IForce.x = pRspeed.x + -oRspeed.x IForce.y = pRspeed.y + -oRspeed.y IForce.z = pRspeed.z + -oRspeed.z if(IForce.x < 0) then IForce.x = -IForce.x; end if(IForce.y < 0)then IForce.y = -IForce.y; end if(IForce.z < 0)then IForce.z = -IForce.z; end speed = math.max(IForce.x,IForce.y,IForce.z); return speed; end ---------- Cash Bonus ---------- while true do wait() if script.Parent.Throttle ~= 0 and script.Parent.Steer == 0 then Speed = GetSpeed(script.Parent); if speed > 20 then local cash = data:findFirstChild("Money") cash.Value = cash.Value + cashadd local dist = stats:findFirstChild("Miles") dist.Value = dist.Value + distadd end end end
I'm putting this in the vehicle's driver seat
edit: there is no output error
First of all, I don’t understand why you need such a complex calculation for vehicle speed when you can just get it with one line in many different ways.
Well, there’s 3 conditions your car should meet before cash upgrades. One is that, VehicleSeat throttle should be above 0, but alongside that, steer should be zero, and speed should be above 20. If one of them is not true, cash and distance will not upgrade. You should try and print each one of them in while true do, before if statements and see if they would be true if they where inside if statement. You will see that one of them is not what would meet if Statements condition. Probably that would be either speed or steer(maybe steer is never absolute zero)
---------- Cash Bonus ---------- while true do wait() Speed = GetSpeed(script.Parent); print(Speed) print(script.Parent.Throttle) print(script.Parent.Steer) if script.Parent.Throttle ~= 0 and script.Parent.Steer == 0 then if speed > 20 then local cash = data:findFirstChild("Money") cash.Value = cash.Value + cashadd local dist = stats:findFirstChild("Miles") dist.Value = dist.Value + distadd end end end