I’m currently trying to make a spectate system where you click a button, and it changes the player’s cameramode to ‘Classic’ and you can spectate others. I’ve managed to do the other part of it but I can’t do the keycode part.
This is the code: (long!)
local spectateT = script.Parent:WaitForChild("SpectateToggle")
local spectateGUI = script.Parent:WaitForChild("SpectateGUI")
--Frame Variables
spectateT.Position = UDim2.new(.005, 0, .5, 0)
spectateGUI.Position = UDim2.new(.4, 0, 1, 0)
local cameramode = game.Players.LocalPlayer.CameraMode
--Setting Positions before Game
local db = false
local toggle = false
local position
function getPos()
for i, v in pairs(game.Players:GetPlayers()) do
if v.Name == game.Players.LocalPlayer.Name then
position = i
return position
end
end
end
--Prints Position of Player relative to Table of Players
function keyOpen(inputObject)
if inputObject.KeyCode == Enum.KeyCode.H then
openCamera()
end
end
function openCamera()
if db == false then
toggle = true
pos.Value = getPos()
db = true
spectateT:TweenPosition(UDim2.new(-.1, 0, .5, 0), Enum.EasingDirection.In, Enum.EasingStyle.Back, .5)
wait(.5)
spectateGUI:TweenPosition(UDim2.new(.4, 0, .8, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, .5)
wait(1)
db = false
end
end
local name = spectateGUI.User
local pos = script.Parent.Pos
for i, v in pairs(spectateT:GetChildren()) do
if v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
openCamera()
end)
end
end
--When Specate Icon is Clicked
local name = spectateGUI.User
local pos = script.Parent.Pos
for i, v in pairs(spectateT:GetChildren()) do
if v:IsA("ImageButton") then
keyOpen()
end
end
-- When H is pressed
local cam = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:wait() or plr.Character
wait()
cam.CameraSubject = char:WaitForChild("Humanoid")
pos.Changed:connect(function()
if pos.Value == getPos() then
name.Text = "Yourself"
cam.CameraSubject = char:WaitForChild("Humanoid")
else
local plr2 = game.Players:GetPlayers()[pos.Value]
name.Text = plr2.Name
local char2 = plr2.Character or plr2.CharacterAdded:wait()
cam.CameraSubject = char2:WaitForChild("Humanoid")
end
end)
--Changes Views when Position is Changed
function decreases()
local number = pos.Value
number = number - 1
if number <= 0 then
number = #game.Players:GetPlayers()
pos.Value = number
else
pos.Value = number
end
end
--Decreases Increment when Pressed or Clicked [Q]
function increases()
local number = pos.Value
number = number + 1
if number > #game.Players:GetPlayers() then
number = 1
pos.Value = number
else
pos.Value = number
end
end
--Increases Increment when Pressed or Clicked [E]
function onKeyPress(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.Q and toggle == true and gameProcessedEvent == false then
decreases()
elseif inputObject.KeyCode == Enum.KeyCode.E and toggle == true and gameProcessedEvent == false then
increases()
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
--Clicking E or Q on keyboard
for i, v in pairs(spectateGUI:GetChildren()) do
if v.Name == "Back" and v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
decreases()
end)
elseif v.Name == "Next" and v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
decreases()
end)
end
end
--Clicking on the Buttons
for i, v in pairs(spectateGUI:GetChildren()) do
if v.Name == "Cancel" and v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
if db == false then
toggle = false
db = true
cam.CameraSubject = char:WaitForChild("Humanoid")
spectateGUI:TweenPosition(UDim2.new(.4, 0, 1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Back, .5)
wait(.5)
spectateT:TweenPosition(UDim2.new(.005, 0, .5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, .5)
wait(1)
db = false
end
end)
end
end
--When Exiting the Spectate UI
And there is an error, it is:
Players.TeamDreams123.PlayerGui.SpectateGui.LocalScript:24: attempt to index nil with 'KeyCode'
its because inputObject isnt assigned as a keycode. when you call it you are calling the function your calling it as keyOpen(). there needs to be something as an argument or it will be nil
you could do that but it would require you to change the opencamera function too. I meant that you should replace what I sent with the part u say keyOpen() at.
local spectateT = script.Parent:WaitForChild("SpectateToggle")
local spectateGUI = script.Parent:WaitForChild("SpectateGUI")
--Frame Variables
spectateT.Position = UDim2.new(.005, 0, .5, 0)
spectateGUI.Position = UDim2.new(.4, 0, 1, 0)
local cameramode = game.Players.LocalPlayer.CameraMode
--Setting Positions before Game
local db = false
local toggle = false
local position
function getPos()
for i, v in pairs(game.Players:GetPlayers()) do
if v.Name == game.Players.LocalPlayer.Name then
position = i
return position
end
end
end
--Prints Position of Player relative to Table of Players
function keyOpen() -- This function got changed
game:GetService("UserInputService").InputBegan:Connect(function(input)
keyOpen(input)
end)
end
function openCamera()
if db == false then
toggle = true
pos.Value = getPos()
db = true
spectateT:TweenPosition(UDim2.new(-.1, 0, .5, 0), Enum.EasingDirection.In, Enum.EasingStyle.Back, .5)
wait(.5)
spectateGUI:TweenPosition(UDim2.new(.4, 0, .8, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, .5)
wait(1)
db = false
end
end
local name = spectateGUI.User
local pos = script.Parent.Pos
for i, v in pairs(spectateT:GetChildren()) do
if v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
openCamera()
end)
end
end
--When Specate Icon is Clicked
local name = spectateGUI.User
local pos = script.Parent.Pos
for i, v in pairs(spectateT:GetChildren()) do
if v:IsA("ImageButton") then
keyOpen()
end
end
-- When H is pressed
local cam = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:wait() or plr.Character
wait()
cam.CameraSubject = char:WaitForChild("Humanoid")
pos.Changed:connect(function()
if pos.Value == getPos() then
name.Text = "Yourself"
cam.CameraSubject = char:WaitForChild("Humanoid")
else
local plr2 = game.Players:GetPlayers()[pos.Value]
name.Text = plr2.Name
local char2 = plr2.Character or plr2.CharacterAdded:wait()
cam.CameraSubject = char2:WaitForChild("Humanoid")
end
end)
--Changes Views when Position is Changed
function decreases()
local number = pos.Value
number = number - 1
if number <= 0 then
number = #game.Players:GetPlayers()
pos.Value = number
else
pos.Value = number
end
end
--Decreases Increment when Pressed or Clicked [Q]
function increases()
local number = pos.Value
number = number + 1
if number > #game.Players:GetPlayers() then
number = 1
pos.Value = number
else
pos.Value = number
end
end
--Increases Increment when Pressed or Clicked [E]
function onKeyPress(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.Q and toggle == true and gameProcessedEvent == false then
decreases()
elseif inputObject.KeyCode == Enum.KeyCode.E and toggle == true and gameProcessedEvent == false then
increases()
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
--Clicking E or Q on keyboard
for i, v in pairs(spectateGUI:GetChildren()) do
if v.Name == "Back" and v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
decreases()
end)
elseif v.Name == "Next" and v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
decreases()
end)
end
end
--Clicking on the Buttons
for i, v in pairs(spectateGUI:GetChildren()) do
if v.Name == "Cancel" and v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
if db == false then
toggle = false
db = true
cam.CameraSubject = char:WaitForChild("Humanoid")
spectateGUI:TweenPosition(UDim2.new(.4, 0, 1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Back, .5)
wait(.5)
spectateT:TweenPosition(UDim2.new(.005, 0, .5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, .5)
wait(1)
db = false
end
end)
end
end
--When Exiting the Spectate UI
that wouldnt work. sorry for confusing you I meant that u replace the one that was on line 57 (or close to line 57) this wouldnt work cuz the function gonna keep calling itself
I’m gonna cut down the script to what has changed now, to make it less confusing.
function keyOpen()
game:GetService("UserInputService").InputBegan:Connect(function(input)
keyOpen(input)
end)
end
local name = spectateGUI.User
local pos = script.Parent.Pos
for i, v in pairs(spectateT:GetChildren()) do
if v:IsA("ImageButton") then
keyOpen()
end
end
-- When H is pressed
ok all you have to do is make function keyOpen() as it previously was which is
function keyOpen(inputObject)
if inputObject.KeyCode == Enum.KeyCode.H then
openCamera()
end
end
and the second one should be
local name = spectateGUI.User
local pos = script.Parent.Pos
for i, v in pairs(spectateT:GetChildren()) do
if v:IsA(“ImageButton”) then
game:GetService(“UserInputService”).InputBegan:Connect(function(input)
keyOpen(input)
end)
end
end
I dont see whats wrong. the only thing I see that might be wrong is that if you press the next button the decrease() function happens and not the increase() one. if this doesnt work I sadly wont be able to help you for some time cuz I got to sleep rn and I have school tommorow
game:GetService("UserInputService").InputBegan:Connect(function(input, chat)
if chat then return end
if not input.KeyCode == Enum.Keycode.H then return end
openCamera()
end)
Ive fixed the script, not sure if you need it anymore but here it is:
local spectateT = script.Parent:WaitForChild("SpectateToggle")
local spectateGUI = script.Parent:WaitForChild("SpectateGUI")
--Frame Variables
spectateT.Position = UDim2.new(.005, 0, .5, 0)
spectateGUI.Position = UDim2.new(.4, 0, 1, 0)
local cameramode = game.Players.LocalPlayer.CameraMode
--Setting Positions before Game
local db = false
local toggle = false
local position
function getPos()
for i, v in pairs(game.Players:GetPlayers()) do
if v.Name == game.Players.LocalPlayer.Name then
position = i
return position
end
end
end
--Prints Position of Player relative to Table of Players
function keyOpen(inputObject, pos) --//pos is here and I explained why in another line after this one
if inputObject.KeyCode == Enum.KeyCode.H then
openCamera(pos)
end
end
function openCamera(pos) --//here I put pos which is just as an arguement so that you can add one later
if db == false then
toggle = true
pos.Value = getPos()
db = true
spectateT:TweenPosition(UDim2.new(-.1, 0, .5, 0), Enum.EasingDirection.In, Enum.EasingStyle.Back, .5)
wait(.5)
spectateGUI:TweenPosition(UDim2.new(.4, 0, .8, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, .5)
wait(1)
db = false
end
end
local name = spectateGUI.User
local pos = script.Parent.Pos
for i, v in pairs(spectateT:GetChildren()) do
if v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
openCamera(pos) --//pos is here because pos isnt define before so you need to send it for it to be in the function
end)
end
end
--When Specate Icon is Clicked
local name = spectateGUI.User
local pos = script.Parent.Pos
for i, v in pairs(spectateT:GetChildren()) do
if v:IsA("ImageButton") then
game:GetService("UserInputService").InputBegan:Connect(function(input)
keyOpen(input, pos) --//to transfer pos to the keyopen function too
end)
end
end
-- When H is pressed
local cam = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait() --// you should do plr.Character before plr.CharacterAdded:Wait()
wait()
cam.CameraSubject = char:WaitForChild("Humanoid")
pos.Changed:connect(function()
if pos.Value == getPos() then
name.Text = "Yourself"
cam.CameraSubject = char:WaitForChild("Humanoid")
else
local plr2 = game.Players:GetPlayers()[pos.Value]
name.Text = plr2.Name
local char2 = plr2.Character or plr2.CharacterAdded:wait()
cam.CameraSubject = char2:WaitForChild("Humanoid")
end
end)
--Changes Views when Position is Changed
function decreases()
local number = pos.Value
number = number - 1
if number <= 0 then
number = #game.Players:GetPlayers()
pos.Value = number
else
pos.Value = number
end
end
--Decreases Increment when Pressed or Clicked [Q]
function increases()
local number = pos.Value
number = number + 1
if number > #game.Players:GetPlayers() then
number = 1
pos.Value = number
else
pos.Value = number
end
end
--Increases Increment when Pressed or Clicked [E]
function onKeyPress(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.Q and toggle == true and gameProcessedEvent == false then
decreases()
elseif inputObject.KeyCode == Enum.KeyCode.E and toggle == true and gameProcessedEvent == false then
increases()
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
--Clicking E or Q on keyboard
for i, v in pairs(spectateGUI:GetChildren()) do
if v.Name == "Back" and v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
decreases()
end)
elseif v.Name == "Next" and v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
increases()
end)
end
end
--Clicking on the Buttons
for i, v in pairs(spectateGUI:GetChildren()) do
if v.Name == "Cancel" and v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
if db == false then
toggle = false
db = true
cam.CameraSubject = char:WaitForChild("Humanoid")
spectateGUI:TweenPosition(UDim2.new(.4, 0, 1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Back, .5)
wait(.5)
spectateT:TweenPosition(UDim2.new(.005, 0, .5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, .5)
wait(1)
db = false
end
end)
end
end
--When Exiting the Spectate UI
btw most of the stuff I changed are commented with --//