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

Tycoon script for door solution (functions)?

So I am making a tycoon, and I have this:

local StringValue = script.Parent:WaitForChild("Value")
local SurfaceGui = script.Parent:WaitForChild("SurfaceGui")
local TextLabel = SurfaceGui:WaitForChild("TextLabel")
local Door = script.Parent	

Door.Touched:connect(function(x)
	local playername = x.Parent.Name
	StringValue.Value = playername
	TextLabel.Text = playername.."'s Tycoon" 
end) 

The problem with this is that if someone else touches the door the tycoon will be considered theirs. Is there a way to stop the function from running after it is touched by one person?

Thanks.

2 answers

You are able to disconnect the event

Example:-

local part = Instance.new('Part', game.Workspace)
part.Anchored = true

local con = nil
con = part.Touched:connect(function(hit)
	print('Part was touched by', hit.Parent.Name)
	con:disconnect() -- disconnects the event so I can no longer be fired
	print('Event disconnected')
end)

There are other ways such as checking a value ect. You will need to decide which method is best at this one will require you to re-add the event when the tycoon resets upon the player leavening unless just re-ran the script.

I hope this helps, please comment if you do not understand how / why this code works.

Just add a "Used = false" or something. Once touched, it turns true. Here is what I mean:

local StringValue = script.Parent:WaitForChild("Value")
local SurfaceGui = script.Parent:WaitForChild("SurfaceGui")
local TextLabel = SurfaceGui:WaitForChild("TextLabel")
local Door = script.Parent  
local USED = false

Door.Touched:connect(function(x)
if USED == false then -- checks if used
if  x.Parent.Humanoid ~= nil then -- checks if it has humanoid, I recommend adding this
USED =  true -- now it is used
    local playername = x.Parent.Name
    StringValue.Value = playername
    TextLabel.Text = playername.."'s Tycoon" 
end -- an extra end due to the first "if"
end -- an extra end due to the second "if"
end) 

I'm a bit sleepy rn and don't have studio open, so I'm not sure if I made any silly mistakes myself, but that should be better. Also, if you do this, make sure to have it reset once the player leaves the game :)

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