I finally solved it! (Is this code sloppy?)

Hello fellow devs!
I have been working on an aspect of my game, and I finally solved it! I won’t deny that the code is kind of sloppy. Let me explain what the point of it is.

I’m making a tycoon game, and I wanted to make it so that once you claim one, you can’t claim another. Here is the layout:

next to each tycoon that is claimable, there is a proximity prompt that allows you to claim it. In the workspace, I made parts for functional tycoons so far.
image

in the script for the currency, I added this chunk to set myself on the right track
image

it then checks if that value’s name has been changed like this:


if in fact the name has not been changed it then changes the name to “HasTycoon” so that the other prompts won’t falsely give you that tycoon as well.
Here is what the full script from the prompt looks like

script.Parent.Triggered:Connect(function(plr)

	if plr:FindFirstChildWhichIsA("IntValue").Name == "HasTycoon" then
	else
		plr.Val.Name = "HasTycoon"
		game.ServerStorage.Blue.buyCircle.Parent = game.Workspace
		script.Parent.Enabled = false
		local playerName = Instance.new("IntValue")
		playerName.Parent = script.Parent.Parent
		wait(1)
		playerName.Name = plr.Name


		print(playerName.Name .. " Claimed Blue!")

	end
	end)

this then goes on to a Billboard GUI that displays your user name…" 's Tycoon"
what I’m asking is, is there a more efficient way to do this?
Thanks in advance!

Just give the player a boolean value and set it to true when they’ve already claimed a tycoon.

I’m honored that you named your IntValue after me.


Jokes aside, couldn’t you just use a BoolValue instead of using the name of an IntValue and use a StringValue for the player’s name instead of an IntValue’s name as the player’s name? I feel like that would be more efficient.

Here’s the optimized code for you. I haven’t tested it, but try this:

-- Wherever your previous code for the IntValue was would be replaced by this
local val = Instance.new("BoolValue")
val.Parent = plr
val.Name = "HasTycoon"
val.Value = false

local ServerStorage = game:GetService("ServerStorage")
local HasTycoon = --[ Put the path to the Boolean Value here ]--

script.Parent.Triggered:Connect(function(plr)

	if HasTycoon.Value == true then
    -- Code here
	else
		HasTycoon.Value == true
		ServerStorage.Blue.buyCircle.Parent = game.workspace
		script.Parent.Enabled = false
		local playerName = Instance.new("StringValue")
		playerName.Parent = script.Parent.Parent
		wait(1)
		playerName.Value = plr.Name


		print(playerName.Value .. " Claimed Blue!")

	end
end)
1 Like

image

this will error if it cannot find a child of that type. you need to check if it exists before trying to get its name

I tried this but it sadly failed. I must have been doing s/thing wrong

But it does exist because in the leaderstats script, I added the intvalue to the player

Exactly what I’m looking for!
Thanks!
Now the game won’t lag as much!

1 Like