i want to make kinda a capturing system where each second a player is touching the capturing part it gives the player 1 point. i actually thought i solved this with a localscript but i realized when adding leaderstats with a local script the rest of the game can see how much points the player has. this means i have to do this with a normal script.
Script:
script.Parent.Touched:Connect(function(hit)
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
if user ~= nil then
user.leaderstats.Capturing.Value = 1
wait(0.05)
user.leaderstats.Capturing.Value = 1
end
end)
script.Parent.Touched:Connect(function(P)
local user = game.Players:GetPlayerFromCharacter(P.Parent)
if user ~= nil then
while wait(1) do
if user.leaderstats.Capturing.Value == 1 then
user.leaderstats.Points.Value += 1
end
end
end
end)
You can tell by the script that i was trying to make this system with Connect functions but this doesnt work because it activates a ton of them at once and not once every second.
I think the only way to solve this is by making a function that can only run once every second while the part is being touched but i dont know how to do that.
script.Parent.Touched:Connect(function(P)
local touching = true
local user = game.Players:GetPlayerFromCharacter(P.Parent)
if user ~= nil then
while touching do
wait(1)
user.leaderstats.Points.Value += 1
end
end
end)
sorry im also a beginner and i didnt test this with leaderstats, so does it add multiple points into the leaderstat in one second? Just trying to see where its going wrong
oh yeah thats right thats because touching is set to true the first time it gets touched it never gets set to false after stepping off, so it would increase without the part being touched. Im tryna figure out how to solve that
local GettingPoints = false
script.Parent.Touched:Connect(function(hit)
if GettingPoints then
return
end
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
if user then --// if user has the same effect as if user ~= nil
GettingPoints = true
user.leaderstats.Points.Value += 1
task.wait(1)
GettingPoints = false
end
end)
If I understood your script correctly, then this should work. The idea here is the variable GettingPoints will prevent the function from running if it is true, as it will just return instead of running the function, and it will be set to true as soon as you start getting points, and set to false once the wait is over.
Hope this helps
Oh, okay, if you want it to loop, something like this might work (I hope):
local Touching = false
script.Parent.Touched:Connect(function(hit)
if Touching then
return
end
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
if user then
Touching = true
while Touching do
user.leaderstats.Points.Value += 1
task.wait(1)
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
if user then
Touching = false
end
end)
it works the first time i touch it but when i get off it and get back on it 75% of the time it adds multiple every second and 25% of the time it acts normal, Also when a player equips an item it gives the player 100s of points
local part = script.Parent
local cooldown = false
part.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
if humanoid and not cooldown then
user.leaderstats.Points.Value += 1
cooldown = true
task.wait(1)
cooldown = false
end
end)
I don’t know why it would give the player 100s of points, but if its because its touching the part very rapidly, then adding a new variable like this may work?
local Touching = false
local First = 0
script.Parent.Touched:Connect(function(hit)
if Touching then
return
end
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
First += 1
if user and First == 1 then
Touching = true
while Touching do
user.leaderstats.Points.Value += 1
task.wait(1)
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
if user then
Touching = false
First = 0
end