How Do I Make It One Player Only?

Hello,

I’m really new to Lua & I’m still learning a lot of stuff. Sorry if I sound stupid lol.

I’m making a Tycoon game & I was wondering if I could get some help with this code.

I want it so that once the part is touched once & is claimed by a player. No other players can claim it.

I’m assuming I just have to write something like If #### = false then #### or something but I’m really unsure.

This is my code so far (sorry if it’s bad).

Any help would be appreciated.

You can just use a bool value and see if its claimed or not.

local claimed = false

--Part Touched
if claimed == false then
   --the player can claim it 
  claimed = true
else
   --the part is already claimed
end

Thank you so much! You’re a lifesaver.

It doesn’t work. Here’s the code:

Did I do something wrong?

Your ends… at the last end, at a close parenthesis. Then at the second last end, remove the closing parenthesis. Then delete the third last end.

It’ll look something like this :

Part.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        script.Parent.BillboardGui.TextLabel.Text = (Hit.Parent.Name .. "'s Programming Base!")
        claimed = true
    else
        --Whatever you gotta do here...
    end
end)
1 Like

go to your games place settings and set the max player count to 1

1 Like

He’s talking 'bout something else, if you read above…

1 Like

oh i saw the title and thought he meant something else

1 Like
local claimed = false
local name = script.Parent.BillboardGui.TextLabel
local currentPlayer = Instance.new('StringValue') -- Current player who owns tycoon
currentPlayer.Parent = script.Parent

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') ~= nil then
        if claimed == false then
            claimed = true
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            name.Text = player.Name.."'s Programming Base!"
            currentPlayer.Value = player.Name
        end
    end
end)

-- Setting up a PlayerRemoving event can check if the player left and then clear the tycoon
game.Players.PlayerRemoving:Connect(function(player)
    if currentPlayer.Value == player.Name then
        currentPlayer.Value = nil
        claimed = false
        name.Text = 'Claim'
    end
end)
2 Likes

Still doesn’t work :joy:

Someone corrected me, just remove the ) at line 13 and you should be fine (if you’re still confused I fixed the script above, just copy and paste it).

1 Like

Also you need another ) in the last end.

1 Like

It works but when the player who claimed leaves the game, the tycoon doesn’t become claimable again. I setup a RemoteEvent. Is this right?

image image

No, PlayerRemoving is its own event and not a remote event. You can just leave it as is.

Also replace the touched event with this, let me know if it works:

script.Parent.Touched:Connect(function(hit)
    if claimed == false then
        if hit.Parent:FindFirstChild('Humanoid') ~= nil then
            claimed = true
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            name.Text = player.Name.."'s Programming Base!"
            currentPlayer.Value = player.Name
        end
    end
end)
1 Like

I tested it & it still doesn’t work.

Here’s the full code:

Hey there!

The script isn’t working, because it tries to set StringValue’s Value to nil, which isn’t possible and it results into an error.

If you replace the PlayerRemoving part of the code with this:

Code
game.Players.PlayerRemoving:Connect(function(player)
    if currentPlayer.Value == player.Name then
        currentPlayer.Value = ""
        claimed = false
        name.Text = 'Claim'
    end
end)

then it should work just fine.

Hope this helped, have nice rest of your day!

1 Like

here is the code:

Code.txt (779 Bytes)

1 Like

if you make it so that when the part is touched it teams the player to lets say red team if you then do script.Disabled = True then it shouldn’t let any other players get teamed red. It would also be a good idea to turn the collision off and part.visible = false. Tell me if this doesn’t work so I can do some tests on it to try fix it. I am not certain how to change the team but I think you an use Player.Team = Team name this might not be the case though.