local Buy = game.Workspace.Buy
local BuyValue = Buy.Value.Value
game.Players.PlayerAdded:Connect(function(joining) --When the player joins the game…
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = joining
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 1
Coins.Parent = leaderstats
BuyValue = 200
local Part1 = game.Workspace.Part1
Buy.Touched:Connect(function(touched)
local humanoid = touched.Parent:FindFirstChild("Humanoid")
if humanoid and Coins.Value >= 200 then
Buy:Destroy()
Part1.Transparency = 0
local TotalScore = Coins.Value - BuyValue --Value - Part value
print(TotalScore)
Coins.Value = TotalScore
else
Buy.BrickColor = BrickColor.Random()
end
end)
local CollecterCashButton = workspace.Collector
local CashValueScreen = workspace.Collector.CashValueScreen.SurfaceGui.TextLabel
local CashScreen = 0
while true do
CollecterCashButton.Touched:Connect(function(touch)
local humanoid = touch.Parent:FindFirstChild("Humanoid")
if humanoid then
Coins.Value = Coins.Value + CashScreen
CashScreen = 0
end
end)
Also, please post the code correctly next time. Thank you.
Now, about your code, if I understand what you are trying to achieve here, then:
local Buy = game.Workspace.Buy
local BuyValue = 200 -- Adjusted to a fixed value
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0 -- Set starting coins to 0
Coins.Parent = leaderstats
Buy.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
if Coins.Value >= BuyValue then
Buy:Destroy()
local Part1 = game.Workspace.Part1
Part1.Transparency = 0
local TotalCoins = Coins.Value - BuyValue
print(TotalCoins)
Coins.Value = TotalCoins
else
Buy.BrickColor = BrickColor.Random()
end
end
end)
local CollectorCashButton = workspace.Collector
local CashValueScreen = CollectorCashButton.CashValueScreen.SurfaceGui.TextLabel
local CashScreen = 0
CollectorCashButton.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
Coins.Value = Coins.Value + 1 -- Adjusted to always add 1 when touched
CashScreen = 0
end
end)
end)
Cleaned up your code a bit here. It’s especially bad to set up the .Touched connection inside your loop, because it will eat up memory and fire hundreds or thousands of touched events when the collector button is stepped on (depending on how many times the loop has run so far).
I also added debounces to avoid processing events too quickly, especially if deferred signals is enabled.
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local part1 = Workspace.Part1
local collectCashPart = Workspace.Collector
local cashValueLabel = collectCashPart.CashValueScreen.SurfaceGui.TextLabel
local buyPart = Workspace.Buy
local buyCost = buyPart.Value.Value
local function getPlayerFromPart(part)
if not part:IsDescendantOf(Workspace) then
return
end
while part and part.Parent and part.Parent ~= Workspace do
if part.Parent:IsA("Model") then
local player = Players:GetPlayerFromCharacter(part.Parent)
if player then
return player
end
end
part = part.Parent
end
end
Players.PlayerAdded:Connect(function(joiningPlayer)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = joiningPlayer
local coinsLeaderstat = Instance.new("IntValue")
coinsLeaderstat.Name = "Coins"
coinsLeaderstat.Value = 1
coinsLeaderstat.Parent = leaderstats
local wasTouched = false
-- This sets up a connection on the same buyPart as everyone else. Somehow you need to
-- reference an unused buyPart unique for this player. Without seeing the setup of your game,
-- I can't tell you how to do that.
buyPart.Touched:Connect(function(touchingPart)
if wasTouched then
return
end
wasTouched = true
local player = getPlayerFromPart(touchingPart)
if not player then
wasTouched = false
return
end
if player and coinsLeaderstat.Value >= buyCost then
buyPart:Destroy()
part1.Transparency = 0
coinsLeaderstat.Value -= buyCost
print(`{player.DisplayName} bought the part for {buyCost}!`)
else
buyPart.BrickColor = BrickColor.Random()
task.delay(0.5, function()
wasTouched = false
end)
end
end)
local storedCash = 0
local collectCashDebounce = false
-- Again this is the same collect cash part for everyone, typically tycoon games have each player
-- have their own collection part.
collectCashPart.Touched:Connect(function(touchingPart)
if collectCashDebounce then
return
end
local player = getPlayerFromPart(touchingPart)
if not player then
return
end
-- If this were unique to the player, you'd need to check that the touching player is the owner with something like:
-- if player ~= joiningPlayer then
-- return
-- end
collectCashDebounce = true
coinsLeaderstat.Value += storedCash
storedCash = 0
task.delay(0.5, function()
collectCashDebounce = false
end)
end)
while true do
task.wait(1)
storedCash += 1
cashValueLabel.Text = storedCash
end
end)
It’s unclear to me how this is meant to work because you have a static reference to a buy part that is used for everyone. The connections are not checking for the joining player to be the one to touch, it’s just allowing a touch from any player. This is probably the issue you’re experiencing.
Please read the comments in here that I added for a little explanation of the issue.
I fixed up code, by making it look better, and for it to work more smoothly, I also changed the script of the “CollectorCashButton” a little bit, now it adds + 1 to the coins value. you had while loop, and I removed it I don’t understand why you needed it.
I hope this doesn’t come off as rude, but I found your code a bit unclear. I gave it a shot and made some adjustments based on my understanding.
It wasn’t shown in OP’s code snippet, but I inferred that the loop’s purpose is to increase stored cash over time, and stepping on the collection button resets the store to 0 and adds it to your coin leaderstat. See my snippet for a more complete implementation of this.
Is the player meant to have their own unique tycoon area?
If so, then you need to get references to unique purchase and collect buttons instead of using the same one for everyone. Can you explain a bit about how your game is set up in this regard?
yes don’t worry about it. don’t take any of this in a bad way, I am actually glad that you are trying, and for a first ever code done based on a game its not that bad.
So, as the image attached here, i want 4 spots. If a player touches the Green pad which gives the money, i want it to give it to them only. Before, when somebody touched it, it gave the coins to everyone
You’ll need a concept of an “owner” of a plot then. I highly recommend following a tutorial like this one https://www.youtube.com/watch?v=XY6Ig48tAnk which covers in detail how to set up a basic tycoon and track the owner.
If I understand the code correctly, it’s not everyone getting the coins, it’s anyone. That is, the problem is there is no check that the person touching the collection button is the same person who owns the tycoon plot, because there’s no concept of an owner in this game.
That tutorial I linked keeps track of owners by setting an attribute on the tycoon plot model. There’s a number of different ways you could track ownership of a plot, and that is a perfectly good way to do it. So first you need to track who owns the plot, then add a check during the touch that doesn’t process giving coins if the touching player is not the owner.