Ontouch function locally without remote events, is it possible?

I am trying to make a part that will increase a player stat by +1 on touch and also disable it so it can only be used once, I have done this however I can only manage to do it server sided.
I want to be able to complete this criteria locally so it only disables for the player that touched the block.

I believe what I am looking for is a local script that can do the on touch function.

Can this be achieved without remote events?

debounce = false

script.Parent.Touched:connect(function(hit)

	if not debounce then
		debounce = true
		if(hit.Parent:FindFirstChild("Humanoid")~=nil)then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				player.leaderstats.Level.Value += 1
				script.Enabled = false
		end
		debounce = false
	end
end)

I’ve been trying to fix this for a while, I haven’t found anything relevant or that worked on the devforum. Any help is appreciated.

You’d probably be better off doing it on the server because there isn’t a way to do this without at least 1 remote event.

If you want to track if a player already touched the brick, you could keep a list of players who’ve already used that brick and exclude them from gaining more points. Like a blacklist of players. Would look something like this:

local debounce = false
local playerBlacklist = {}

script.Parent.Touched:connect(function(hit)
	local isACharacter = hit.Parent:FindFirstChild("Humanoid")

	if not isACharacter then
		return
	end

	local player = game.Players:GetPlayerFromCharacter(hit.Parent)

	if not debounce and not playerBlacklist[player] then
		debounce = true
		playerBlacklist[player] = true
		player.leaderstats.Level.Value += 1

		task.wait(0.2)
		debounce = false
	end
end)
1 Like

I have made a server script in startercharacterscripts so it sort of works client sided, however I want to make multiple parts that increase the stats. From my own knowledge I’m thinking of just making one script for each part, is there an easier way of doing this? If not i’ll just use the original idea. :smiley:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

humanoid.Touched:Connect(function(hit)
	if hit.Name == "LevelPart" then
		print("Hit F")
		local player = game.Players:GetPlayerFromCharacter(script.Parent)
		player.leaderstats.Level.Value += 1
		script.Enabled = false
	end
end)

Your sample here will fire hundreds if not thousands of times within a very small interval, remember to insert a debounce. You could, if you already know that the parts are present, just make a loop.

local LevelParts = .. -- Define yourself, a folder perhabs
local Debounce = {}

for_, Part in pairs(LevelParts:GetChildren() do
	Part.Touched:Connect(function(Hit)
		local Character = Hit.Parent
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		if not Humanoid or Humanoid.Health == 0 then return end
		local Player = game.Players:GetPlayerFromCharacter(Character)
		if not Player then return end
		if Debounce[Player.Name] then return end
		Debounce[Player.Name] = true
		player.leaderstats.Level.Value += 1
		task.wait(1)
		Debounce[Player.Name] = nil
	end)
end

This script should be located in ServerScriptService

1 Like

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