I am trying to make a freecam script and make the player stay where the player was standing before that player activated the script. I tried 3 methods but it still says "Unable to cast value to Object".
All comments explaining more of how the script works will be in Method 1. Method 2 and 3 are just modifications of Method 1.
Method 1
Using the actual value (for ex:10, 0, 20)
LocalScript: (in StarterGui in case if your wondering.)
local v = true --This just tells if the player is in FreeCam or not.
local Player = game.Players.LocalPlayer --the player
local player = game.Workspace:WaitForChild(Player.Name) --the player in the Workspace
local playercords = nil --basiclly where the torso was last before it activated FreeCam
local sendinfo = game:GetService('ReplicatedStorage').Reciever -- the RemoteEvent
game:GetService("UserInputService").InputBegan:connect(function(key)
sendinfo:FireServer(v, key, Player, player, playercords)
sendinfo.OnClientEvent:connect(function(v2, playercords2) --This just changes the variables here so next time it can send the correct coordinates and other stuff.
v = v2
playercords = playercords2
end)
end)
ServerScript: (in ServerScriptService in case if your wondering.)
local remote = game:GetService("ReplicatedStorage").Reciever --da RemoteEvent
remote.OnServerEvent:connect(function(_, v2, key, Player, player, playercords2, name)
local v = v2
local playercords = {playercords2}
if v and key.KeyCode == Enum.KeyCode.LeftAlt then
print("Fly mode")
playercords = player:FindFirstChild("LowerTorso").Position + Vector3.new(0, 2, 0)
Player.Character = nil
workspace.CurrentCamera.CameraType = "Fixed"
v = false
elseif v and key.KeyCode == Enum.KeyCode.RightAlt then
Player.Character = nil
workspace.CurrentCamera.CameraType = "Fixed"
v = false
elseif v == false and key.KeyCode == Enum.KeyCode.LeftAlt then
print("Fall mode")
workspace.CurrentCamera.CameraType = "Custom"
Player:LoadCharacter()
print("Attempting to teleport a player.")
player:FindFirstChild('LowerTorso').CFrame = CFrame.new(playercords[1])
v = true
elseif v == false and key.KeyCode == Enum.KeyCode.RightAlt then
workspace.CurrentCamera.CameraType = "Custom"
Player:LoadCharacter()
v = true
end
remote:FireClient(name, tostring(v), playercords)
end)
Method 2
Using a part and have the LocalScript find the part's position:
LocalScript:
local v = true
local Player = game.Players.LocalPlayer
local player = game.Workspace:WaitForChild(Player.Name)
local playercords = nil
local sendinfo = game:GetService('ReplicatedStorage').Reciever
local name = Player.Name
game:GetService("UserInputService").InputBegan:connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftAlt or key.KeyCode == Enum.KeyCode.RightAlt then
sendinfo:FireServer(v, key, Player, player, playercords, name)
sendinfo.OnClientEvent:connect(function(v2, playercords2)
v = v2
playercords = playercords2.Position
end)
end
end)
ServerScript:
local remote = game:GetService("ReplicatedStorage").Reciever
remote.OnServerEvent:connect(function(_, v2, key, Player, player, playercords2, name)
local v = v2
local playercords = {playercords2}
if v and key.KeyCode == Enum.KeyCode.LeftAlt then
print("Fly mode")
playercords = player:FindFirstChild("LowerTorso").Position + Vector3.new(0, 2, 0)
Player.Character = nil
workspace.CurrentCamera.CameraType = "Fixed"
v = false
elseif v and key.KeyCode == Enum.KeyCode.RightAlt then
Player.Character = nil
workspace.CurrentCamera.CameraType = "Fixed"
v = false
elseif v == false and key.KeyCode == Enum.KeyCode.LeftAlt then
print("Fall mode")
workspace.CurrentCamera.CameraType = "Custom"
Player:LoadCharacter()
print('wait')
wait(10)
print("Attempting to telerport a player.")
player:FindFirstChild('LowerTorso').CFrame = CFrame.new(playercords[1])
v = true
elseif v == false and key.KeyCode == Enum.KeyCode.RightAlt then
workspace.CurrentCamera.CameraType = "Custom"
Player:LoadCharacter()
v = true
end
Instance.new("Part").Parent = script.Parent
Player.PlayerGui.Name = "PlaceHolder"
Player.PlayerGui.PlaceHolder.Position = Vector3.new(playercords) --The error here says "PlayerGui is not a valid member of Player"
remote:FireClient(name, tostring(v), script.Parent.PlaceHolder:GetFullName())
end)
Method 3
Using a table:
LocalScript:
local v = true
local Player = game.Players.LocalPlayer
local player = game.Workspace:WaitForChild(Player.Name)
local playercords = nil
local sendinfo = game:GetService('ReplicatedStorage').Reciever
local name = Player.Name
game:GetService("UserInputService").InputBegan:connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftAlt or key.KeyCode == Enum.KeyCode.RightAlt then
sendinfo:FireServer(v, key, Player, player, playercords, name)
sendinfo.OnClientEvent:connect(function(v2, playercords2)
v = v2
playercords = playercords2[1]
end)
end
end)
ServerScript:
local remote = game:GetService("ReplicatedStorage").Reciever
remote.OnServerEvent:connect(function(_, v2, key, Player, player, playercords2, name)
local v = v2
local playercords = {playercords2}
if v and key.KeyCode == Enum.KeyCode.LeftAlt then
print("Fly mode")
playercords = player:FindFirstChild("LowerTorso").Position + Vector3.new(0, 2, 0)
Player.Character = nil
workspace.CurrentCamera.CameraType = "Fixed"
v = false
elseif v and key.KeyCode == Enum.KeyCode.RightAlt then
Player.Character = nil
workspace.CurrentCamera.CameraType = "Fixed"
v = false
elseif v == false and key.KeyCode == Enum.KeyCode.LeftAlt then
print("Fall mode")
workspace.CurrentCamera.CameraType = "Custom"
Player:LoadCharacter()
print('wait')
wait(10)
print("Attempting to telerport a player.")
player:FindFirstChild('LowerTorso').CFrame = CFrame.new(playercords[1])
v = true
elseif v == false and key.KeyCode == Enum.KeyCode.RightAlt then
workspace.CurrentCamera.CameraType = "Custom"
Player:LoadCharacter()
v = true
end
Instance.new("Part").Parent = script.Parent
script.Parent.Part.Name = "PlaceHolder"
script.Parent.PlaceHolder.Position = Vector3.new(playercords)
remote:FireClient(name, tostring(v), script.Parent.PlaceHolder:GetFullName())
end)

1 comment
Thanks!