So I'm making a house system and how can I detect if the player leaves? This is my code.

local Players = game:GetService("Players")
function onTouch(part) 
	local humanoid = part.Parent:FindFirstChild("Humanoid") 
	if (humanoid ~= nil) then	-- if a humanoid exists, then
		local player = Players:GetPlayerFromCharacter(part.Parent)
		if not player then return end
		if player.leaderstats.Cash > 299 then
			player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 300
		end
		while wait (15) do
			local findingplayer = game.Players:FindFirstChild(player.Name)
		end
	end 
end

script.Parent.Part.Touched:connect(onTouch)

is there something else I can do that would work better? perhaps playerremoving?

2 comments

0

Im gonna need more info, is the house an object in a map or the whole map? Also line 11 will simply grab all players, not just the ones that line 2 found.

0

Its an object in the map