Stand Selection handler

so i’m working on a rewrite of one of my older games, yes it is a jojo game…, as a fun project to learn scripting and efficiency better. i feel like i have done better with efficiency but i would like to know from those better than me, if this code is ‘efficient’, or ‘clean’. i would also want to know if this is ‘safe’ from exploits. this is the way my code works:

you have a StandString value inside of your character, that is your stand. when you choose a character on the stand selection screen it will change your standstring value.
when you perform a move it makes sure your StandString is the stand you need to perform the move

uis.InputBegan:Connect(function(key,chatting)
	if key.KeyCode ==  Enum.KeyCode.One and character.StandString.Value == "Magicians Red"then
		remotes.crossfire:FireServer()
	end
end)

we have a event on the server that checks for if your StandString has changed, and if so, it fires a bindable event to the server.

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function (character)
		local standstring = character:WaitForChild("StandString")
		if standstring.Value == "Star Platinum" then
			stand_create:Fire(standstring, player)
		end
		if standstring.Value == "Magicians Red" then
			stand_create:Fire(standstring, player)
		end
    end)
end)

then, when that bindable event is recieved, we give them their stand.

stand_create.Event:Connect(function(standstring, player)
	print("got event")
	if standstring.Value == "Star Platinum" then
        --my code here
	end
	if standstring.Value == "Magicians Red" then
		--my code here
	end
	local charbruh = standstring.Parent
	local savestring = standstring
	charbruh.Humanoid.Died:Connect(function()
		print("thisboi died")
		player.CharacterAppearanceLoaded:Connect(function(c)
			player.Character.StandString.Value = savestring.Value
		end)
	end)
end)

screenshots:
stand string:
Screen Shot 2020-10-02 at 7.56.27 AM
selection gui:
Screen Shot 2020-10-02 at 7.56.43 AM
server script layout:
Screen Shot 2020-10-02 at 7.56.01 AM

what do you guys think? :grinning:

		if standstring.Value == "Star Platinum" then
			stand_create:Fire(standstring, player)
		end
		if standstring.Value == "Magicians Red" then
			stand_create:Fire(standstring, player)
		end

For this part of the code, to make it more efficient and easier to expand in the future, you may want to store a table of valid stands and simply check if its in the table, as so:

local STANDS = {"Magicians Red",  "Star Platinum"}
if table.find(STANDS, standstring.Value) ~= nil then
	stand_create:Fire(standstring, player)
end

Not only is this shorter but it will make it easier in the future for you to add more stands without having to clutter the code with if statements

If you want to make it even better, you can have the list of valid stands be a ModuleScript in ReplicatedStorage so that you can also verify that it’s a valid stand on the client side instead of checking explicitly whether it’s valid

For your bindable event code, make sure to disconnect your connections when you are done with them, so you don’t potentially cause a memory leak

Those are the major things I can see, good job though on using remote events and server-side validation to make it more secure!

3 Likes

I want to make a JoJo game, but I have no idea how to make it more organized like you have it, could you perhaps make a tutorial on how to make a JoJo stand with summon and moves?

Well jojo games, aren’t that hard to make. (or at least the basics) It’s basically just welding another character to your player and yeah, from there it’s pretty self explanatory.

Well my scripts always break and i have like 15
serverscripts for every stand move

Okay, thanks

(character limit)

Not only is table.find expensive, using an if statement is pretty much O(1). So unless you have a lot of if statements and complicated operations that you want to see decoupled elsewhere, just use it. If anything, use a dictionary lookup. If statements are efficient, that’s not the issue, it is negligble beyond compare to compare the two but table.find is definitely not the way to go even if you want readability…

1 Like

Two if statements are the same runtime complexity as table.find on a table of two elements. The only difference is readability

I just benchmarked it and that’s not remotely true. Secondly, if readability is a concern, you wouldn’t use table.find. An if statement is much more explicit when n <= 2. n = number of cases.