Hello! I am trying to create a system where when a player touches a part, they are awarded a win.
However, it is not adding the wins to the leaderstat. I’ve been attempting a lot of different things but I can’t get it to work.
I have a script inside the workspace which contains this code:
game.Players.PlayerAdded:Connect(function(player)
-- Creates leaderstats if it doesn't exist
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
-- Creates TimePlayed value if it doesn't exist
local timePlayed = leaderstats:FindFirstChild("TimePlayed")
if not timePlayed then
timePlayed = Instance.new("IntValue")
timePlayed.Name = "Speed"
timePlayed.Value = 0
timePlayed.Parent = leaderstats
end
local playerwins = leaderstats:FindFirstChild("PlayersWins")
if not playerwins then
playerwins = Instance.new("IntValue")
playerwins.Name = "Wins"
playerwins.Value = 0
playerwins.Parent = leaderstats
end
-- Updates TimePlayed every second
while player do
wait(1)
timePlayed.Value = timePlayed.Value + 1
end
while player do
local part = game.Workspace:FindFirstChild("EasyModeWin")
-- Check if the part exists
if part then
-- Connect the Touched event
part.Touched:Connect(function(hit)
print("part touched") -- DOES NOT GET PRINTED IN THE OUTPUT
-- Check if the player touched the part
if hit.Parent:FindFirstChild("Humanoid") then
-- Get the player from the hit part
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
-- Check if the player exists and the leaderstats folder exists for the player
if player and player:FindFirstChild("leaderstats") then
-- Get the player's wins stat
local playerwins = player.leaderstats:FindFirstChild("Wins")
-- Check if the playerwins stat exists
if playerwins then
-- Increase the player's wins by one
playerwins.Value = playerwins.Value + 1
end
end
end
end)
end
end
end)
ignore the speed part
The issue is that the script doesn’t register when the part is touched. I have it so its prints something when touched but it doesn’t print anything in the output.
I tested your code and it seems that from the second player loop, local part does not work inside the Players.PlayerAdded event nor the player loop but outside of both by itself.
Also, add a debounce so that the wins value does not continuously increase unexpectedly.
local part = workspace:FindFirstChild("EasyModeWin")
if part then
local db = false
-- Connect the Touched event
part.Touched:Connect(function(hit)
print("part touched")
-- Check if the player touched the part
if hit.Parent:FindFirstChild("Humanoid") then
-- Get the player from the hit part
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
-- Check if the player exists and the leaderstats folder exists for the player
if player and player:FindFirstChild("leaderstats") then
if db then return end
db = true
-- Get the player's wins stat
local playerwins = player.leaderstats:FindFirstChild("Wins")
-- Check if the playerwins stat exists
if playerwins then
-- Increase the player's wins by one
playerwins.Value = playerwins.Value + 1
end
wait(3) -- How long do you want to wait until the player can touch it again?
db = false
end
end
end)
end
If you have a while loop in the middle of a function, ask yourself this: Do I want to function to stop here until this task finishes? Which in your case that while loop will never finish.
You can have the while loop in the same place, but not halt your function by encasing it in a task.spawn() function.
Right now the first whole loop will go on forever and never run the rest of the code.
Example:
task.spawn(function()
-- put your while loop here
end)
-- make another task.spawn function for your other loop
Let me know how this goes, I actually had this issue earlier today.
task.spawn(function()
local part = game.Workspace:FindFirstChild("EasyModeWin")
-- Check if the part exists
if part then
-- Connect the Touched event
part.Touched:Connect(function(hit)
print("part touched") -- DOES NOT GET PRINTED IN THE OUTPUT
-- Check if the player touched the part
if hit.Parent:FindFirstChild("Humanoid") then
-- Get the player from the hit part
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
-- Check if the player exists and the leaderstats folder exists for the player
if player and player:FindFirstChild("leaderstats") then
-- Get the player's wins stat
local playerwins = player.leaderstats:FindFirstChild("Wins")
-- Check if the playerwins stat exists
if playerwins then
-- Increase the player's wins by one
playerwins.Value = playerwins.Value + 1
end
end
end
end)
end
end)
end)
However, If I change both this function and the other function to task.wait, the wins leaderstat increases but the speed doesn’t.
The problem is two endless loops, the script will never get to the second one. You also shouldn’t do so many if’s. I added a cooldown
local winners = {}
local part = game.Workspace:WaitForChild("EasyModeWin")
local players = game:GetService('Players')
part.Touched:Connect(function(hit)
print("part touched") -- DOES NOT GET PRINTED IN THE OUTPUT
-- Check if the player touched the part
if players:GetPlayerFromCharacter(hit.Parent) and not table.find(winners, players:GetPlayerFromCharacter(hit.Parent).UserId) then
-- Get the player from the hit part
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local playerwins = player.leaderstats:FindFirstChild("Wins")
playerwins.Value = playerwins.Value + 1
table.insert(winners, player.UserId)
task.wait(1) -- cooldown
table.remove(winners, winners[player.UserId])
end
end)
game.Players.PlayerAdded:Connect(function(player)
-- Creates leaderstats if it doesn't exist
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
-- Creates TimePlayed value if it doesn't exist
local timePlayed = leaderstats:FindFirstChild("TimePlayed")
if not timePlayed then
timePlayed = Instance.new("IntValue")
timePlayed.Name = "Speed"
timePlayed.Value = 0
timePlayed.Parent = leaderstats
end
local playerwins = leaderstats:FindFirstChild("PlayersWins")
if not playerwins then
playerwins = Instance.new("IntValue")
playerwins.Name = "Wins"
playerwins.Value = 0
playerwins.Parent = leaderstats
end
-- Updates TimePlayed every second
while player do
wait(1)
timePlayed.Value = timePlayed.Value + 1
end
end)