LocalScript Animation and Part not working

I am new at coding in lua and using roblox studio but anyways I wanna make a animated steering wheel and that the player can play animation wiht the hands its (R6) and its in a localscript its client sided… is there anyway to make it function in a localscript and that the player animation and the steering wheel animation is so other people in the game can see it?

The localscript is in the StarterPlayerScripts:


local player = game:GetService("Players").LocalPlayer
local part = game.Workspace[player.Name.."_Jeep"].Wheel.Union

local rotateSpeed = 90 

local sAnimationId = "15436527787" 
local aAnimationId = "15436548044" 

local sAnimTrack = nil
local aAnimTrack = nil

local function playAnimation(animationId)
	local animController = part:FindFirstChildOfClass("Humanoid") or part:FindFirstChildOfClass("AnimationController")
	if animController then
		local animation = Instance.new("Animation")
		animation.AnimationId = "rbxassetid://" .. tostring(animationId)
		local animTrack = animController:LoadAnimation(animation)
		animTrack:Play()
		return animTrack
	end
end

local function stopAnimation(animTrack)
	if animTrack then
		animTrack:Stop()
		animTrack:Destroy()
	end
end

local function onKeyPress(input)
	if input.KeyCode == Enum.KeyCode.S then
		sAnimTrack = playAnimation(sAnimationId)
		game.ReplicatedStorage.Turning:FireServer("PlayAnimation", "S")
	elseif input.KeyCode == Enum.KeyCode.A then
		aAnimTrack = playAnimation(aAnimationId)
		game.ReplicatedStorage.Turning:FireServer("PlayAnimation", "A")
	end
end

local function onKeyRelease(input)
	if input.KeyCode == Enum.KeyCode.S then
		stopAnimation(sAnimTrack)
		game.ReplicatedStorage.Turning:FireServer("StopAnimation", "S")
	elseif input.KeyCode == Enum.KeyCode.A then
		stopAnimation(aAnimTrack)
		game.ReplicatedStorage.Turning:FireServer("StopAnimation", "A")
	end
end

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
game:GetService("UserInputService").InputEnded:Connect(onKeyRelease)
3 Likes

Due to client sided scripts not replicating to everyone else, you would have to fire a remote event and handle all the steering stuff on the server so everyone can see it. In case you don’t know how to do that you put a remote event object in game.ReplicatedStorage and you can utilize it with the following code.

game:GetService("ReplicatedStorage").RemoteEvent:FireServer()

Then make a script in ServerScriptService and have the following code

game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(player)
    —Server sided steering animation code goes her
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.