Using dictionaries help

I’m trying to switch from collection service to check character states like “Stunned”, “Attacking”, “Ragdoll”, to dictionaries, i rarely ever used them but i know they are very useful. This is what i got for now:

and its erroring me:
and this is where the checkcharstate function is called:

can someone help me out write it better and fix the problem?

The code is kinda messy, but I’ll try my best to help you out.
So the error you’re getting simple states that charState[id] is set to nil. Doing nil[“Stunned”] is wrong, and therefore throws you that error. Conclusion: charState[id] has not been initialised properly.

How to fix the issue? Maybe SetCharState() has not been called before using CheckCharState(), thus the dictionary returning nil.
Overall, whenever your table/dictionary seems to have weird values, I would always print it and then see what’s going on.

In terms of keeping your code relatively clean and easy to debug, I would recommend naming things properly. When I first read the variable “things” in function.SetCharState(things), I didn’t know what I was expecting.
On another personal level, I dislike the idea of abbreviating your variables. Instead of “plr”, do “player”. Instead of “dura”, do “duration”. Looks much better (again, this is personal. I didn’t have to think long for what “dura” meant)

Hope this helps!

i fixed the issue and it worked for actual players, but when i tried attacking the dummy (Which obviously doesnt have a player and a userid) it threw me an error, i printed the CharacterState and it was correctly assigned, now im trying to figure out how to make it if char doesnt have a player, then getattribute(“UserId”) which is complicated :sob: idk if you can help me out

i fixed it, i just added a line of code that goes:

local player = Players:GetPlayerFromCharacter(things.char) or nill
local id = (player ~= nil) and player.UserId or things.char.UserId.Value

so now, if the player is nil it will look inside the char for its id :slight_smile:

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