I have a vehicle in my game and I want it to have a similar first-person mode as jailbreak. (In jailbreak when you press C , the Cframe of the Camera changes to the Cframe of an invisible part inside the car which represents the first-person camera.)
Now, I want the camera Cframe changed only for the player on the driver’s seat but not the entire server’s players and to achieve that I have prepared three scripts.
One, that enables the server event script whenever it detects a player has entered the driver seat.
local Seat = script.Parent
Seat.Changed:Connect(function()
if Seat.Occupant ~= nil then
script.Parent.camscript.Disabled=false
else script.Parent.camscript.Disabled=true
end
end)
Second, a script that fires an event to the client whenever the player in the driver seat presses C
local userInput = game:GetService("UserInputService")
script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
userInput.InputBegan:Connect(function(key, processed)
if key.KeyCode == Enum.KeyCode.C then
game.ReplicatedStorage["CARCAMERA"]:FireClient(player)
print("Pressed C")
end
end)
end)
And the last script in the StarterPlayerScripts receives the fired event and changes the player’s camera,Cframe to that part named sakuracam1.Cframe.
local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("sakuracam1")
game.ReplicatedStorage:WaitForChild("CARCAMERA").OnClientEvent:connect(function()
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = part.CFrame
print("camera to cframe")
end)
But It is not working(The player’s camera Cframe is not changing to the parts Cframe ) when I am running the game and pressing C while sitting in the driving seat.
Can someone please help me to overcome this issue?
I have changed the client script to this. But not working and also In the second script I mentioned in the thread (server event script) I have added a print section where is it supposed to say pressed C whenever I successfully fire to the client but it is not popping up in the output tab.
local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("sakuracam1")
game.Players.LocalPlayer:WaitForChild("CARCAMERA").OnClientEvent:connect(function()
repeat Camera.CameraType = Enum.CameraType.Scriptable wait() until Camera.CameraType == Enum.CameraType.Scriptable
Camera.Cframe = part.Cframe
print("camera to cframe")
end)
local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("sakuracam1")
game.Players.LocalPlayer:WaitForChild("CARCAMERA").OnClientEvent:connect(function()
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = part.CFrame
print("camera to cframe")
end)
Yes, this script is perfect, but I think the problem is In the second script I mentioned in the thread (server event script) I have added a print section where is it supposed to say pressed C whenever I successfully fire to the client but it is not popping up in the output tab. So I think it is not firing to the clint event.
local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("sakuracam1")
game.ReplicatedStorage:WaitForChild("CARCAMERA").OnClientEvent:connect(function()
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = part.CFrame
print("camera to cframe")
end)
And this is the server event script that fires to the client
local userInput = game:GetService("UserInputService")
script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
userInput.InputBegan:Connect(function(key, processed)
if processed then
return
end
if key.KeyCode == Enum.KeyCode.C then
game.ReplicatedStorage["CARCAMERA"]:FireClient(player)
print("Pressed C")
end
end)
end)