Using module scripts to get player state

would it be possible to have this code inside a module script?

RunService.RenderStepped:Connectfunction((IsPlayerMoving) --running the function
if Humanoid.MoveDirection.Magnitude == 0 then
		print("Player is not moving") 
	elseif Humanoid.MoveDirection.Magnitude > 0 then 
		print("Player is moving")
	end
end)

In short I am wondering if there is a way to have one script check all the player states and have it be accessible by every other script?

1 Like

Yes. You can do it like this.

ModuleScript:

local ModuleScript = {}
ModuleScript.Moving = false

RunService.RenderStepped:Connectfunction((IsPlayerMoving) --running the function
if Humanoid.MoveDirection.Magnitude == 0 then
		ModuleScript.Moving = false 
	elseif Humanoid.MoveDirection.Magnitude > 0 then 
		ModuleScript.Moving = true
	end
end)

return ModuleScript

Other Script:

local ModuleScript = require(ModuleScript)

while ModuleScript.Moving do
	print("hehehehaw")
end
2 Likes

it did require some tweaks but it does work now perfectly, ty
this is the working code for anyone that’s wondering

module script:

local PlayerState = {}

	local RunService = game:GetService("RunService")

	local Player = game.Players.LocalPlayer
	local character = Player.Character or Player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")

	PlayerState.Moving = false

	RunService.RenderStepped:Connect(function() --running the function
	if humanoid.MoveDirection.Magnitude == 0 then
		PlayerState.Moving = false 
	elseif humanoid.MoveDirection.Magnitude > 0 then 
		PlayerState.Moving = true
	end
	end)

return PlayerState

local script

local PlayerState = require(module script location)

print(PlayerState.Moving)

I still havent checked if the code interferes with other clients but it works perfectly atleast in studio

You can simulate multiple clients in studio if you go to the Test tab > Clients and servers > select how many players you want to simulate, and then click Start. This opens multiple studio tabs with different clients and a LocalServer to test on.

1 Like

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