I am making a vehicle control system so that it turns toward the mouse. The problem is that this error pops up and I don’t know what the issue is.
Here is the script:
local Seat = script.Parent
local Gyro = Seat.BodyGyro
Seat.ChildAdded:Connect(function()
local Occupant = Seat.Occupant
local Player = game.Players:GetPlayerFromCharacter(Occupant.Parent)
print(Player)
mouse = Player:GetMouse()
end)
while wait() do -- must be added in a loop for it to work
if Seat.Occupant then
Gyro.MaxTorque = Vector3.new(0,math.huge,0)
Gyro.CFrame = CFrame.new(Vector3.new(Seat.Position,mouse.Hit.p))
end
wait(0.1)
end
1 Like
If this is a server script, no matter what, the mouse is always nil
, the mouse can only be obtained from a localscript and the player is the client
That or it’s because the mouse
variable is local to the ChildAdded event in your case, and you’re trying to use it outside of the event, you’d need an outside variable and assign the Getmouse to it
Could be that it is a server script like @EmbatTheHybrid said, or maybe you clicked on the sky which returns nil. Also, I’m not 100% sure, but maybe the “CanTouch” value has something to do with it?
In this case it can’t be due to him pointing at the sky because it sees mouse
as nil
, if it was your case it would say Attempt to index nil with p
Also how would CanTouch be involved?
1 Like
Oh, this is a server script. So I guess I have to make it with a local script sending the info.
1 Like
If you want it to replicate to the server then yea, you’d need to do the replication from Client to Server
What you could do is listen for when someone is sitting, if they’re a player, fire a Remote to tell their client to start sending mouse information. And then when they get up, tell the client to stop sending mouse information
1 Like
For some reason it says nil for the mouse position. Can you see what’s the problem?
Server Script:
local Seat = script.Parent
local Gyro = Seat.BodyGyro
local Start = game.ReplicatedStorage:WaitForChild("Start")
Start.OnServerEvent:Connect(function(player,Position)
Pos = Position
end)
while wait() do
if Seat.Occupant then
print(Pos)
Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
Gyro.CFrame = CFrame.new(Vector3.new(Seat.Position,Pos))
end
wait(0.1)
end
Local Script:
local Start = game.ReplicatedStorage:WaitForChild("Start")
Start.OnClientEvent:Connect(function(player)
local Mouse = game.Players.LocalPlayer:GetMouse()
local Pos = Mouse.Hit.p
Start:FireServer(player,Pos)
end)
1 Like
Again, I see 3 issues
- Your
Pos
variable is local to the OnServerEvent, so it’s always going to be seen as nil
, you need to declare it outside as well
- Don’t fire in the player in FireServer, it does it for you automatically, if you keep it,
Position
will be set to the player you fired in
- I don’t see you telling the client to give the mouse position, the while wait stuff should be in the OnServerEvent with a variable telling the loop it’s time to stop
Probably something like this
local Seat = script.Parent
local Gyro = Seat.BodyGyro
local Start = game.ReplicatedStorage:WaitForChild("Start")
local Pos
local looping = false
Start.OnServerEvent:Connect(function(player,Position)
Pos = Position
looping = true
end)
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local occupant = Seat.Occupant
local player = occupant and game:GetService("Players"):GetPlayerFromCharacter(occupant.Parent)
if not player then
--FireClient Code here to stop the FireServer
looping = false
end
--Put FireClient Code here to start the FireServer
end)
while true do
if Seat.Occupant and looping then
print(Pos)
Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
Gyro.CFrame = CFrame.new(Vector3.new(Seat.Position,Pos))
end
wait(0.1)
end
1 Like
Thank you so much! This was such a great help for my game!
1 Like