I am currently trying to make a simulator which includes a training zone, I’m not sure how you can detect if a player is touching without using a while loop. People have told me to use touched and touchended but I am unsure on how to use it.
This is my current code: (tool)
local tool = script.Parent
local Player = tool.Parent.Parent
local leaderstat = Player.leaderstats
local zone1 = workspace.Psychic
tool.Activated:Connect(function()
if zone1.Touched then
leaderstat.Psychic.Value +=2
else
leaderstat.Psychic.Value +=1
end
end)
local tool = script.Parent
local Player = tool.Parent.Parent
local leaderstat = Player.leaderstats
local PlayersTouching = {}
local zone1 = workspace.Psychic
tool.Activated:Connect(function()
game:GetService("RunService").Heartbeat:Connect(function()
zone1.Touched:Connect(function(Part)
local PlayerTouched = Players:GetPlayerFromCharacter(Part.Parent)
if PlayerTouched == Player then
PlayersTouching[Player] = true
end
end)
zone1.TouchEnded:Connect(function(Part)
local PlayerTouched = Players:GetPlayerFromCharacter(Part.Parent)
if PlayerTouched == Player then
PlayersTouching[Player] = nil
end
end)
end)
task.spawn(function()
while task.wait(1) do
for _, PlayerTouching in pairs(game:GetService("Players"):GetPlayers()) do
if PlayersTouching[PlayerTouching] and PlayerTouching == Player then
leaderstat.Psychic.Value += 2
elseif PlayerTouching == Player then
leaderstat.Psychic.Value += 1
end
end
end
end)
end)
I tried this but it caused lag and didnt stop raising a stat when i click standing anywhere with the tool. If i touch the part then it adds 2 but infinitely grows in numbers.
also sorry for responding 4 days late
so basically,
training game with psychic - you click tool to add psychic, +1 stat, and when you stand in/on a certain part you get +2 stat. Not setting the value of it to 1 or 2.