How can I optimize zed tycoon script

I feel like there’s too much if statements and it looks pretty messy. Also is it normal for scripts to run at 5 rate per second? Can you please help me optimize my code and shorten it if possible?

-- This is a server script
local debounce = false

script.Parent.Head.Touched:connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if debounce == false then
		debounce = true
		if player ~= nil then
			local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
			if PlayerStats ~= nil then
				local ownstycoon = PlayerStats:FindFirstChild("OwnsTycoon")
				print(ownstycoon)
				if ownstycoon ~= nil then
					if ownstycoon.Value == nil then
						if script.Parent.Parent.Parent.Owner.Value == nil then
							if hit.Parent:FindFirstChild("Humanoid") then
								if hit.Parent.Humanoid.Health > 0 then
									script.Parent.Parent.Parent.Owner.Value = player
									ownstycoon.Value = script.Parent.Parent.Parent
									script.Parent.Name = player.Name.."'s Tycoon"
								end
							end
						end
					end
				end
			end
		end
		wait(5)
		debounce = false
	end
end)
1 Like
-- This is a server script
local debounce = false
local Button = script.Parent.Parent -- path to your button
local OwnerValue = script.Parent.Parent.Parent:WaitForChild("Owner")
local DebounceTime = 5 -- set to whatever you want

Button.Touched:connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if debounce == false then
		debounce = true
		if player ~= nil then
			local PlayerStats = game:GetService("ServerStorage").PlayerMoney:FindFirstChild(player.Name)
			if PlayerStats ~= nil then
				local ownstycoon = PlayerStats:FindFirstChild("OwnsTycoon")
				print(ownstycoon)
				if ownstycoon ~= nil and ownstycoon.Value == nil and OwnerValue.Value == nil and hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health > 0 then
					OwnerValue.Value = player
					ownstycoon.Value = script.Parent.Parent.Parent
					script.Parent.Name = player.Name.."'s Tycoon"
				end
			end
		end
		task.wait(DebounceTime)
		debounce = false
	end
end)
5 Likes