A guy I know came to me and asked me to fix a script for him, but I’m not sure how to do it, so I figured I’d take it here. I’ll let him explain it:
I’ve begun work on a game where the main objective is to push x to complete y, but I’ve ran into a problem. I’m using a custom character which involves a special script, alongside a part as the part that moves, with a robot mesh added on top to act as the “body.” It seems to work perfectly in studio, but when I test it ingame, the character spazzes out and flies around.
Studio: https://gyazo.com/94fa4ecab0c2698ae898db0b29d8751c
Ingame: https://gyazo.com/dc97f9a266ccb6dd7b509202ed8df65b
Enabling filtering just breaks the script, and I have no idea what else I can do. The script is a LocalScript located in the StarterPlayerScripts.
The script:
char = workspace:WaitForChild("Char")
w, a, s, d = 0,0,0,0
game:GetService("UserInputService").InputBegan:connect(function(k)
k = k.KeyCode.Name
if k == "W" then
w = 1
elseif k == "A" then
a = 1
elseif k == "S" then
s = 1
elseif k == "D" then
d = 1
elseif k == "Space" then
jump()
end
end)
game:GetService("UserInputService").InputEnded:connect(function(k)
k = k.KeyCode.Name
if k == "W" then
w = 0
elseif k == "A" then
a = 0
elseif k == "S" then
s = 0
elseif k == "D" then
d = 0
end
end)
function jump()
if ground and not jumping then
jumping = true
char.Velocity = v2(char.Velocity, 30)
wait(.3)
jumping = false
end
end
function v2(v3, b)
return Vector3.new(v3.X, b or 0, v3.Z)
end
wait()
workspace.CurrentCamera.CameraType = "Scriptable"
ignore = {char}
ground = false
jumping = false
while 1 do
workspace.CurrentCamera.CFrame = CFrame.new(char.Position + Vector3.new(0,4,5)*3, char.Position)
local dt = game:GetService("RunService").RenderStepped:wait()
local ismove = math.abs(a-d)+math.abs(w-s)>0
local dir = Vector3.new()
if ismove then
dir = CFrame.new(Vector3.new(), Vector3.new(d-a, 0, s-w)).lookVector
end
local f1, f2, f3 = workspace:FindPartOnRayWithIgnoreList(Ray.new(char.Position, Vector3.new(0,-3)), ignore)
ground = f1
if jumping then f1 = nil end
char.BodyForce.Force = dir * 400 - v2(char.Velocity) * 20
+(f1 and Vector3.new(0, ((f2.Y+2) - char.Position.Y)*300 + workspace.Gravity*char:GetMass() - char.Velocity.Y*30 ) or Vector3.new())
end