Your script is all over the place! There's many reasons to why it doesn't work and it has a few things that look unnecessary. First of all, the ".PlayerAdded" and the ":GetPropertyChangedSignal("Occupant")" don't seem at all needed for this in the slightest. What's even worse that that you're nesting the ":Connect()" functions within each other meaning you're redefining them which can seriously slow down your game if done with many other players! I don't see why you need to check if the person who sat down is a player who joined? Especially because all it does is that it checks things but even if something that isn't a player sat on the vehicle seat then it wouldn't be able to do anything in the first place!
A major issue is that "UserInputService" only works in localScripts! LocalScripts only work if it's within a player's character, is in StarterPlayerScrips, or is within a GUI. Your car won't move for everyone else if you move it using only localscripts as those only are for the player to see and interact. LocalScripts run within a player's PC which is needed to check for button presses while Scripts run within Roblox's servers which show everyone what goes on but cannot check for an individual player's button presses.
To show you the bare-bones to at least get it working, I'll construct it for you from square one.
First create a "RemoteEvent" in "ReplicatedStorage" (you will need this to communicate with the game server from your player's PC).
Put this in a Script within whatever model you wanted:
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)
if script.Parent.Occupant == plr.Character.Humanoid then
script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent:GetPrimaryPartCFrame() * CFrame.new(0, 0, -1))
end
end)
Put this in a LocalScript within "StarterPlayerScripts":
UIS = game:GetService("UserInputService")
looped = false
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
looped = true
while looped do
game.ReplicatedStorage.RemoteEvent:FireServer()
wait(.3)
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
looped = false
end
end)
If you have any questions or issues contact me. ;)
7 comments
Now this will move the vehicle by 1 stud each time the "W" key is pressed; you have to press it over and over to make it move a distance! At this point you can edit it to allow the vehicle to repeatedly do this until the player releases the "W" key.
How could I make it so it only moves forward when the player holds W, thanks so much this helped a lot!
Actually nvm I think I figured it out
I have edited it a tad bit to allow you to do this. It's inefficient but it gets the job done.
That doesn't work
My apologies, there was two mistakes. The first one was that I forgot to scope into "input.KeyCode" and the second one being that I can't set a CFrame value with a Vector3 value (".LookVector"). I have changed both scripts and should now be working. :)
Don't forget to set a PrimaryPart within the model (and make sure the front of that PrimaryPart is facing the correct direction)!