AFK Zone not awarding Xp

I’m trying to create a block that awards all players standing on it 5 Xp every 5 seconds, but when I try to detect the players currently standing on the brick and award them it won’t work. I’ve tried just giving the local player Xp and it also doesn’t work.

local AFKZone = script.Parent
local coolDownTime = 5
local PlayerWhoTouched = {}

AFKZone.Touched:Connect(function(other)
	if other:IsA("Player") then
		PlayerWhoTouched[other] = true
	end
end)

while true do
	wait(coolDownTime)

	for player in pairs(PlayerWhoTouched) do
		local character = player.Character
		if character then
			local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart then
				local touchingParts = humanoidRootPart:GetTouchingParts()

				local isOnAFKZone = false
				for _, part in pairs(touchingParts) do
					if part == AFKZone then
						isOnAFKZone = true
						break
					end
				end

				if isOnAFKZone then
					local leaderstats = player:FindFirstChild("leaderstats")
					if leaderstats then
						local Xp = leaderstats:FindFirstChild("Xp")
						if Xp then
							Xp.Value = Xp.Value + 5
						end
					end
				else
					PlayerWhoTouched[player] = nil
				end
			end
		end
	end
end

The issue might be with the way you’re trying to detect if a player is touching the AFKZone.

Here’s a code I wrote, which should work:

local AFKZone = script.Parent
local coolDownTime = 5
local PlayersOnZone = {}

AFKZone.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        PlayersOnZone[player] = true
    end
end)

AFKZone.TouchEnded:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        PlayersOnZone[player] = nil
    end
end)

while true do
    wait(coolDownTime)

    for player in pairs(PlayersOnZone) do
        local leaderstats = player:FindFirstChild("leaderstats")
        if leaderstats then
            local Xp = leaderstats:FindFirstChild("Xp")
            if Xp then
                Xp.Value += 5
            end
        end
    end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.