Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My vehicle 'W' key drive script isn't working?

I'm trying to make a drive script but it isn't working, my script is inside of a seat which is in a model which is in workspace.

game.Players.PlayerAdded:Connect(function(plr)
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
	if script.Parent.Occupant == plr.Name then
		local UIS = game:GetService("UserInputService")
		UIS.InputBegan:Connect(function(input, gpe)
			if not gpe then
				if input.KeyCode == Enum.KeyCode.W then
					script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.CFrame + 1)
				end
			end
		end)
	end
	end)
end)

Also if anyone could please help me with detecting when they are holding W and when they stop I would appreciate it! Thanks!

2 answers

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 --If you have multiple models of this then it checks if the player who asked to move the vehicle is the one riding it (the value that ".Occupant" returns is a humanoid, not a player's name!)
		script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent:GetPrimaryPartCFrame() * CFrame.new(0, 0, -1)) --This sets the CFrame to the direction the model's PrimaryPart is facing and multiplying it by 1 indicating that it would move that direction by 1 stud
	end
end)

Put this in a LocalScript within "StarterPlayerScripts":

UIS = game:GetService("UserInputService") --This gets the player's local inputs (such as keyboard buttons pressed)
looped = false --If looping should happen

UIS.InputBegan:Connect(function(input) --Gets the player's input
    if input.KeyCode == Enum.KeyCode.W then --Checks if the input is the same as the "W" key
        looped = true --Sets looping to true
        while looped do --Repeats a loop until the looping is false
            game.ReplicatedStorage.RemoteEvent:FireServer() --Sends a signal to the "RemoteEvent" to tell the server you called on it
            wait(.3) --A pause so the game doesn't crash (the higher the slower your car moves)
        end
    end
end)

UIS.InputEnded:Connect(function(input) --Gets the player's input
    if input.KeyCode == Enum.KeyCode.W then --Checks if the input is the same as the "W" key
        looped = false --Sets the looping to false resulting in the loop stopping
    end
end)

If you have any questions or issues contact me. ;)

7 comments

0

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.

0

How could I make it so it only moves forward when the player holds W, thanks so much this helped a lot!

0

Actually nvm I think I figured it out

0

I have edited it a tad bit to allow you to do this. It's inefficient but it gets the job done.

0

That doesn't work

0

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. :)

0

Don't forget to set a PrimaryPart within the model (and make sure the front of that PrimaryPart is facing the correct direction)!

@lazycoolboy500 I have these 2 scripts but they don't work.

Local script inside starterplayerscripts

UIS = game:GetService("UserInputService") --This gets the player's local inputs (such as keyboard buttons pressed)

UIS.InputBegan:Connect(function(began) --Gets the player's input
    if began == Enum.KeyCode.W then --Checks if the input is the same as the "W" key
        game.ReplicatedStorage.VehicleW:FireServer("began") --Sends a signal to the "RemoteEvent" to tell the server you called on it
UIS.InputEnded:Connect(function(ended)
	if ended == Enum.KeyCode.W then
		game.ReplicatedStorage.VehicleW:FireServer("ended")
	end
end)
    end
end)

script inside drivers seat inside model

game.ReplicatedStorage.VehicleW.OnServerEvent:Connect(function(plr, state) --Runs when the "RemoteEvent" gets called on (it always sends the player who called on it first)
    if script.Parent.Occupant == plr.Character.Humanoid then --If you have multiple models of this then it checks if the player who asked to move the vehicle is the one riding it (the value that ".Occupant" returns is a humanoid, not a player's name!)
if state == "began" then
script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent:GetPrimaryPartCFrame().LookVector * 0.1) --This sets the CFrame to the direction the model's PrimaryPart is facing and multiplying it by 1 indicating that it would move that direction by 1 stud
		else
	script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent:GetPrimaryPartCFrame().LookVector * 0)
end
end
end)

Thanks for all the help!

This discussion is preserved as it was. New answers, comments, and votes are closed.