I have a script that detects if someone is in battle, at which the player's movement is disabled to allow walkto commands and animations. I'm using a very simple line of code that has worked many times before, however it's having a problem with just this script. This script used to set humanoid walkspeed and jumppower to 0, but that interrupts walkto commands, which is why I disabled player movement with :GetControls(). This is the script (Inside StarterPlayerScripts):
-- >>:InBattle Local Script local plr = game:GetService("Players").LocalPlayer local char = plr.Character or plr.CharacterAdded:wait() local controls = require(game:GetService("Players").LocalPlayer.PlayerScripts.PlayerModule):GetControls() local pBattleUI = plr.PlayerGui:WaitForChild("BattleUI") while wait(0.01) do local playerstats = plr:WaitForChild("PlayerStats") local inbat = playerstats:FindFirstChild("InBattle") if inbat.Value then --If player is then controls.Disable() pBattleUI.EnemyUI.Enabled = true pBattleUI.StatsUI.Enabled = true else -- if not controls:Enable() pBattleUI.EnemyUI.Enabled = false pBattleUI.StatsUI.Enabled = false end end
The output comes with an error (Use view source to see easier),
07:21:13.390 Players.kodymarcey.PlayerScripts.PlayerModule.ControlModule:232: attempt to index nil with 'activeController' - Client - ControlModule:232 07:21:13.390 Stack Begin - Studio 07:21:13.390 Script 'Players.kodymarcey.PlayerScripts.PlayerModule.ControlModule', Line 232 - function Disable - Studio - ControlModule:232 07:21:13.390 Script 'Players.kodymarcey.PlayerScripts.InBattleScript', Line 11 - Studio - InBattleScript:17 07:21:13.390 Stack End - Studio
Specifically Players.kodymarcey.PlayerScripts.PlayerModule.ControlModule:232: attempt to index nil with 'activeController' is the error and I can still move my player when triggering the if statement. I've made a few alterations (which didn't work) and a test which would be referencing the player module with a local, then making another local and adding :GetControls(), writing controls.Enable(false) instead of controls.Disable(), adding a delay before the script, and I made a test script to disable player movement, which worked and is literally the same lines I added to the script.
local controls = require(game:GetService("Players").LocalPlayer.PlayerScripts.PlayerModule):GetControls() controls.Disable()
This script worked, however when I inserted it into the InBattle Script, it pops up with the error in the output. I appear to be having trouble with both Enable() and Disable(), however the Enable() at the else works (I tried adding controls.Enable() before the while loop as a test and this ended with an error as well). Triggering the inbat.Value if statement and firing controls.Disable() creates an error. A few possibilities for this error, is that there's a problem with the control module, I need to reference the controls local differently, or the way I trigger the if statement by teleporting the player (Setting player position) when triggering a touch event. If you can help me and need to know more, then just ask, thank you in advance!
I solved it myself, putting the controls local before the Enable() and Disable() commands worked.