Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I fix an income script which works inconsistently?

I'm having problems trying to get this script to work consistently. It is supposed to check that person touching it owns the tycoon and if so it will check if they have enough money to complete a purchase. If they have enough money it will increase the income (The value of which is stored in the 'IntValue' shown in the script) and then increase the cost of the upgrade for the next purchase. The main issues seem to be that it will take a long time before executing or it will just break and I have no idea why.

Does anyone know what a potential problem is and how I can go about fixing it??? Thank you

local tycoon = script.Parent.Parent.Parent
local owner = tycoon.Owner -- StringValue for the owner's name
local income = tycoon.Income -- IntValue where income is stored
local debounce = false
local cost = 100 -- Starting price
local costAdd = 115 -- Price increase each purchase
local incomeAdd = 35 -- Income increase each purchase
local times = 4 -- Number of upgrades possible
local timeNum = 0
local ServerStorage = game:GetService("ServerStorage")
local Gui = ServerStorage.MessageGui

script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)

if player ~= nil and debounce == false then

if player and not player.PlayerGui:FindFirstChild(Gui.Name) then
        Gui:Clone().Parent = player.PlayerGui

debounce = true
if owner.Value ~= player.Name then
player.PlayerGui.MessageGui.Message.Alert.Text = "You are not the owner of this tycoon!"

wait(3) -- Wait time to be touched again
player.PlayerGui.MessageGui:Destroy()
debounce = false

return end

if player.leaderstats.Money.Value < cost then
local mn = cost-player.leaderstats.Money.Value
player.PlayerGui.MessageGui.Message.Alert.Text = "Insufficient funds! You need $" ..mn.. " more!"

wait(3) -- Wait time to be touched again
player.PlayerGui.MessageGui:Destroy()
debounce = false

return end

player.leaderstats.Money.Value = player.leaderstats.Money.Value - cost
income.Value = income.Value + incomeAdd
cost = cost + costAdd
timeNum = timeNum + 1
player.PlayerGui.MessageGui.Message.Alert.Text = "Upgrade complete! Income is now $" ..income.Value.. "!"

script.Parent.Parent.Name = "Upgrade Owner's Income - $" ..cost
if timeNum == times then
for i = 0.4, 1, 0.01 do
wait()
script.Parent.Transparency = i
debounce = true
end
script.Parent.Parent:Destroy()
end
wait(3) -- Wait time to be touched again
player.PlayerGui.MessageGui:Destroy()
debounce = false
end
end
end)

9 comments

0

Hope my answer helps you out! For any additional questions post them in the comments under my answer.

0

I edited my answer at the bottom to hopefully assist you.

0

If you do use a LocalScript to activate the gui you will need to keep it somewhere other than serverstorage. ServerStorage cannot be accessed by the client or a LocalScript.

0

If you do use a LocalScript to activate the gui you will need to keep it somewhere other than serverstorage. ServerStorage cannot be accessed by the client or a LocalScript.

0

Remember that I said that LocalScripts cannot access serverstorage. You will have to store the remote event in something like replicated storage which the client/local script can access

0

If I use a Localscript inside the button the user must touch, the button won't work at all.

0

I have also moved the MessageGui to ReplicatedStorage

0

could you post the script either in an edit to this question or a new question?

0

If I helped you out please accept my answer.

1 answer

✓ Accepted answer

Here is my answer:

-- Always properly indent your code!

local tycoon = script.Parent.Parent.Parent
local owner = tycoon.Owner -- StringValue for the owner's name
local income = tycoon.Income -- IntValue where income is stored
local debounce = false
local cost = 100 -- Starting price
local costAdd = 115 -- Price increase each purchase
local incomeAdd = 35 -- Income increase each purchase
local times = 4 -- Number of upgrades possible
local timeNum = 0
local ServerStorage = game:GetService("ServerStorage")
local Gui = ServerStorage.MessageGui
local players = game:GetService("Players") -- the recommended method for getting the players service

script.Parent.Touched:Connect(function(hit)
	
	if hit.Parent:FindFirstChild("Humanoid") then -- want to make sure it is a player touching
		
		local player = players:GetPlayerFromCharacter(hit.Parent)

		if player and not debounce then -- more efficient ways of checking

			if not player.PlayerGui:FindFirstChild(Gui.Name) then -- you already checked for player a couple lines ago
       			Gui:Clone().Parent = player.PlayerGui

				debounce = true
				if owner.Value ~= player.Name then
					player.PlayerGui.MessageGui.Message.Alert.Text = "You are not the owner of this tycoon!"

					wait(3) -- Wait time to be touched again
					player.PlayerGui.MessageGui:Destroy()
					debounce = false
				else -- use else/elseif statements instead of return end if you can
					if player.leaderstats.Money.Value < cost then
						local mn = cost-player.leaderstats.Money.Value
						player.PlayerGui.MessageGui.Message.Alert.Text = "Insufficient funds! You need $" ..mn.. " more!"

						wait(3) -- Wait time to be touched again
						player.PlayerGui.MessageGui:Destroy()
						debounce = false
					else
						player.leaderstats.Money.Value = player.leaderstats.Money.Value - cost
						income.Value = income.Value + incomeAdd
						cost = cost + costAdd
						timeNum = timeNum + 1
						player.PlayerGui.MessageGui.Message.Alert.Text = "Upgrade complete! Income is now $" ..income.Value.. "!"

						script.Parent.Parent.Name = "Upgrade Owner's Income - $" ..cost
						if timeNum == times then
							for i = 0.4, 1, 0.01 do
								wait()
								script.Parent.Transparency = i
								debounce = true 		
							end
							script.Parent.Parent:Destroy()
						end
						wait(3) -- Wait time to be touched again
						player.PlayerGui.MessageGui:Destroy()
						debounce = false
					end
				end
			end
		end
	end
end) --properly indenting allows you to see how many ends you need

-- I hope this helps you out!

Edit:

You should not be accessing the gui from a ServerScript. Instead you should use a remote event and fire the client. An event listener in a LocalScript can open the gui and display what you need it to. You can send arguments which allow you to tell the gui what to say. Make sure that when you :FireClient() you use player as the first argument so it knows what player to affect. I also think that this will fix your error.

Have a great day scripting!

3 comments

0

For some reason the 'MessageGui' won't clone to the users PlayerGui. The error comes from line 18, any reason why?

0

So on line 25 I should send a remote event to a script located in ServerScriptStorage which will clone the MessageGui into the users PlayerGui? Is that correct? Btw to get the Gui to appear the user must simply stand on a button.

0

I can't get the sending and receiving scripts to connect with each other, there's always some sort of error e.g. The second script won't fire. What code do I need in each and where should they both be

This discussion is preserved as it was. New answers, comments, and votes are closed.