How do I make it so that it goes up for everyone? Right now only the last person who joins gets their stats increased, even if someone else touches the Baseplate. This script is in Server Script Service.
local players = game:GetService("Players")
local floor = workspace.Baseplate
local touched = false
local Time = 0
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local Time1 = leaderstats:WaitForChild("Time")
local bestTime = leaderstats:WaitForChild("BestTime")
local function touch(plr)
local human = plr.Parent:FindFirstChild("Humanoid")
pcall(function()
local timeNum = players:GetPlayerFromCharacter(plr.Parent).PlayerGui.TimeGUI
if human and not touched then
touched = true
while touched do
wait(1)
Time = Time + 1
print(Time)
timeNum.TimeValue.Text = Time
Time1.Value = Time
if Time1.Value > bestTime.Value then
bestTime.Value = Time1.Value
end
if human.Health == 0 then
wait(1)
touched = false
Time = 0
timeNum.TimeValue.Text = Time
Time1.Value = Time
end
end
end
end)
end
floor.Touched:Connect(touch)
end)
Touched is only operating for the humanoid :FindFirstChild(“Humanoid”) returns
Ideally you would want to coroutine the function so it does it for every player that joins.
This is because every time a player joins it runs the function for that player. It isn’t a coroutine or spawned so it is a single instance so it wont operated as you are wanting it to.
It doesn’t activate again because the function is stuck with “touched = true” and cannot activate until touched is no longer true. You’ll have to do a handful of changes, not just a coroutine. I’d recommend tagging the player instead of using a boolean.
local players = game:GetService("Players")
local floor = workspace.Baseplate
local Time = 0
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local Time1 = leaderstats:WaitForChild("Time")
local bestTime = leaderstats:WaitForChild("BestTime")
local function touch(plr)
local human = plr.Parent:FindFirstChild("Humanoid")
pcall(function()
local timeNum = players:GetPlayerFromCharacter(plr.Parent).PlayerGui.TimeGUI
if human and not touched then
local tag = Instance.new("BoolValue",plr.Parent.Humanoid) --you can parent this to whatever, just make sure to change the while loop to match.
tag.Name = "Touched"
while plr.Parent.Humanoid:FindFirstChild("Touched") do
wait(1)
Time = Time + 1
print(Time)
timeNum.TimeValue.Text = Time
Time1.Value = Time
if Time1.Value > bestTime.Value then
bestTime.Value = Time1.Value
end
if human.Health == 0 then
wait(1)
tag:Destroy() --or plr.Parent.Humanoid:FindFirstChild("Touched"):Destroy()
Time = 0
timeNum.TimeValue.Text = Time
Time1.Value = Time
end
end
end
end)
end
floor.Touched:Connect(touch)
end)
You might also have to spawn the loop, but I don’t think that’s required? Do tell if it doesn’t work, I kinda cranked this out without too much looking. If it still seizes up you might have to do:
spawn(function()
while plr.Parent.Humanoid:FindFirstChild("Touched") do
--code here
end
end)
function the player from the player touched by doing hit.Parent
Infomation here:
location.Touched:Connect(function(hit) -- change `location` to your part location
local HumanoidTouched = hit.Parent:FindFirstChild("Humanoid")
if HumanoidTouched then
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
-- code here
end
end)
@minimic2002, @WooleyWool, @FFFile, @FlamingGuards
I think that a better visual understanding might help you guys know what I am trying to achieve. In this first Gyazo GIF, you can see that once a player touches a baseplate, their time goes up, and everything works fine. I am using simple leaderstats too.
I was wondering how to change my script so that if say Player 1 touches, only Player 1’s stats go up. And same for any x amount of players in the game. And also if someone else touches, then a random player’s stats shouldn’t go up. Sorry for not posting the videos before. I intended to. It’s just I was in a rush to get this out for quick feedback. However, I realized that a rework of the script might be needed. Anyways, hopefully you guys see this, and let me know if you get any answer for my predicament. Thanks again for replying in the first place.
(If I pinged you and you didn’t wan to be pinged, I apologize. I just pinged everyone who replied)
I’ll rework your script completely to accomplish what you wanted, but be warned this is probably not the most efficient method since I cranked it out in a few minutes:
local players = game:GetService("Players")
local Floor = game.Workspace.Baseplate
local tagged = {} --Don't touch this, it's used to track players touching the floor, keep it empty.
function tags(player) --This function finds a player in the tagged table and returns its position in the table if it exists
for i = 1, #tagged do
if tagged[i] == player then
return i
end
end
return nil
end
Floor.Touched:connect(function(hit)
if hit and hit.Parent then --Sometimes a player might leave the second they touch the baseplate, which can result in hit being nil. This prevents errors.
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Humanoid.Health > 0 then --Make sure the player is alive
local player = players:GetPlayerFromCharacter(hit.Parent)
if player then
if tags(player) == nil then --We don't want to tag a player who's already tagged
table.insert(tagged,player) --Add the player into our tagged table
end
end
end
end
end
end)
--Remove this block of code if you don't need it, I wasn't sure if you did.
Floor.TouchEnded:connect(function(hit) --Untag the player when they stop touching the floor
if hit and hit.Parent then
if hit.Parent:FindFirstChild("Humanoid") then
local player = players:GetPlayerFromCharacter(hit.Parent)
if player then
if tags(player) then --Check if player is tagged
table.remove(tagged,tags(player)) --Untag the player.
end
if hit.Parent.Humanoid.Health <= 0 then --Check if the player is dead
local leaderstats = player:WaitForChild("leaderstats")
local Time = leaderstats:WaitForChild("Time")
Time.Value = 0 --Reset time if so.
end
end
end
end
end)
--
while wait(1) do --Loop to give players who are touching the baseplate their points
for _,player in pairs(tagged) do --Go through all tagged players in the tagged table
if player then
if players:FindFirstChild(player.Name) then --Make sure the player is still in the game
local leaderstats = player:WaitForChild("leaderstats")
local Time = leaderstats:WaitForChild("Time")
local bestTime = leaderstats:WaitForChild("BestTime")
local timeNum = player.PlayerGui.TimeGUI
Time.Value = Time.Value + 1
if Time.Value > bestTime.Value then
bestTime.Value = Time.Value
end
timeNum.TimeValue.Text = Time.Value
if player.Character then
if player.Character.Humanoid.Health <= 0 then --Check if the player is dead
Time.Value = 0
timeNum.TimeValue.Text = Time.Value
if tags(player) then --Check if player is tagged
table.remove(tagged,tags(player)) --Untag the player (since they're dead).
end
end
end
end
end
end
end
I tested it with two players, and it seems to work just like you wanted! I also added in a portion for if the player stops touching the floor, but you can remove that entire block of code if you don’t need it (I would probably remove it).
Also I didn’t test the gui part of the code since I don’t have the gui to test. You’ll have to work that out if it doesn’t work.