I was just wondering why this line does not seem to be adding up/changing position.
for i,v in pairs(script.Parent:GetChildren()) do if v:IsA("TextButton") then if v.Name ~= "Fire" then v.MouseButton1Click:connect(function() if not catbounce and P.Name == game.Workspace.GameMaster.Value then catbounce = true wait() if game.Workspace:WaitForChild("ActiveCamera") then local Camera = game.Workspace:FindFirstChild("ActiveCamera") Camera.CFrame = CFrame.new(Camera.Position) + Vector3.new(tonumber(v.Value.Value))-- Problem is that it does not move the part print("Moved to " .. tostring(game.Workspace.ActiveCamera.Position)) print("Added " .. tostring(v.Value.Value)) wait(.25) end catbounce = false end end) end
I want "Camera" to move position when a gui is clicked by the value thats in the gui Btw "Camera" is a part
First, let's make the code cleaner. Here's what I did:
:FindFirstChild
(don't use :FindFirstChild
when you aren't actually checking if the result is nil
)if workspace:WaitForChild
-- :WaitForChild
can't return nil
. We can just eliminate that whole if
game.Workspace.ActiveCamera
should just be Camera
, we have the variable for itcatbounce
to a meaningful name, declare it at the top, start as true
if
checks in the for
loop by using and
local canClick = true function click(v) if canClick and P.Name == game.Workspace.GameMaster.Value then canClick = false wait() local Camera = workspace:WaitForChild("ActiveCamera") Camera.CFrame = CFrame.new(Camera.Position) + Vector3.new(tonumber(v.Value.Value)) -- Not moving part? print("Moved to " .. tostring(Camera.Position)) print("Added " .. tostring( tonumber(v.Value.Value) ) ) wait(.25) canClick = true end end for i, v in pairs(script.Parent:GetChildren()) do if v.Name ~= "Fire" and v:IsA("TextButton") then v.MouseButton1Click:connect(function() click(v) end) end end
Now this is much easier to read and understand.
I'm guessing the issue is that tonumber(v.Value.Value)
is nil
. Vector3.new(nil)
will be the same as Vector3.new()
, which is Vector3.new(0, 0, 0)
; translating by the origin won't do anything.
Note that if you give Vector3.new
just one number it will use it as the x
value, and the other two (y
(up), z
) will be 0.
Look at what is printed if you use
print(v.Value.Value, tonumber(v.Value.Value) )
tonumber
will return nil
if the string isn't actually a number (e.g. tonumber("5c")
or tonumber("c5")
will return nil
)
You also probably shouldn't be using CFrame.new(Camera.Position)
; that will eliminate any rotation that the camera had before. Instead use this (it's briefer too)
Camera.CFrame = Camera.CFrame + Vector3.new(tonumber(v.Value.Value))
EDIT
If you claim that v.Value.Value
is a Vector3, you definitely shouldn't be using tonumber
or Vector3.new
with it. Just use v.Value.Value
!