Touch event not working




local ServerStorage = game:GetService("ServerStorage")
local Configuration = require(ServerStorage.Configurations)
local Debounce = false
local Collect = game:GetService("ReplicatedStorage").CollectEvent
local Part = script.Parent

Part.Touched:Connect(function(hit)
	local human = hit.Parent:findFirstChild("Humanoid")
	if (human ~= nil) and Debounce == false then
		Debounce = true
		print("Touch")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		Collect:FireServer(player,Part)
	end
end)

the part the contains this local script is spawn on the client side and I dont know if thats the problem.
robloxapp-20230609-1100549.wmv (1.4 MB)

2 Likes

Maybe try switching from a LocalScript to a ServerScript?

2 Likes

I wont be able to fire the event
the event makes it so it colllects your coins

Are you getting any errors in the output window?

I don’t think so, have you tried it out at least?

n it doesnt even render something touching the part

Also you never turn off the debounce so this can only run once, do you plan on destroying it on the server?

1 Like

yeah I have attempted that im not too familiar with events

yes its suppose to destroy it on the server

1 Like

Trying adding a print statement before you intialize the human variable, so we can see if thats actually running

touch event doesnt print anything

It would make more sense for this to be registered on the server.

Using a remote event here means that an exploiter could repeatedly fire that event to gain a bunch of coins.

Instead, register hits on the server and use a remote event to remove the coin on the client:

local part = game.Workspace.Part

local debounce: boolean

function on_hit(other_part: Part)
    if other_part.Parent:FindFirstChildOfClass("Humanoid") and not debounce then
        local PLYRS: Players = game:GetService("Players")
        local plyr: Player = PLYRS:GetPlayerFromCharacter(other_part.Parent)
        event:FireClient(plyr, part)
    end
end

part.Touched:Connect(on_hit)

… then, do something with the part on the client:

function remove_coin(part: Part)
    if not part then return end
    part:Destroy()
end

event.OnClientEvent:Connect(remove_coin)

Or, arguably, just handle both on the client. If the coins aren’t independent to each client, just remove them after updating a player’s coin value on the server!

I dont think he should remove the coin on the client, he should let the coin be handled on the server and also destroyed on the server, he should only use a remote event to fire to the client if he wants to add effects

Is CanTouch set to true or no? If its set to true then this is pretty odd

1 Like

Yeah, I thought about that after posting - hence the last edit at the bottom. Thanks!

Maybe instead of using a .Touched since that isnt working just have magnitudes checks on the client running in a heartbeat loop and then fire to the server that they want to collect the coin, but have the debounce for each player on the server so players cant possibly spam remotes and you can also check if the player is close to a coin aswell

You can just use one heartbeat loop for every single coin that may be in workspace, or well whereever you may store your coins

Actually, I have an idea, you can use a for loop, and a coroutine, and then wait for whatever name your part is, and what object type it is, and then do the touched event.

I might post a sample script of what i mean

Both of these codes are using server scripts:

Touch Event (SeverSided in ServerScriptService):

local ServerStorage = game:GetService("ServerStorage")
local Configuration = require(ServerStorage.Configurations)
local Debounce = false
local Collect = game:GetService("ReplicatedStorage").CollectEvent

for _, eachcoin in pairs(workspace:GetChildren()) do
	if eachcoin:IsA("Part") and eachcoin.Name == 'coin' then
		coroutine.resume(coroutine.create(function()
			eachcoin.Touched:Connect(function(hit)
				local human = hit.Parent:FindFirstChild("Humanoid")
				if eachcoin ~= nil and (human ~= nil) and Debounce ~= true then
					Debounce = true
					local player = game.Players:GetPlayerFromCharacter(hit.Parent)
					print(player.Name)
					Collect:FireServer()
					Debounce = false
				end
			end)
		end))
	end
end

Is it alright if i see the other script that you have?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BreakEvent = ReplicatedStorage.BreakEvent
local DropGem = require(ReplicatedStorage.DropGem)

BreakEvent.OnClientEvent:Connect(function(Box)
	DropGem.DropCurrency(Box.CFrame, ReplicatedStorage.GemDrop, 10)
	Box.Parent:Destroy()
end)

StarterPlayerScripts

local Collect = game:GetService("ReplicatedStorage").CollectEvent

local Configuration = {
	GEM_COLLECT = 1 * 2

Collect.OnClientEvent:Connect(function(player, part)
	part:Destroy()
	player.leaderstats.Gems.Value = player.leaderstats.Gems.Value + Configuration.GEM_COLLECT
end)

StarterPlayerScripts

local Collect = game:GetService("ReplicatedStorage").CollectEvent
local part = script.Parent

function on_hit(other_part: Part)
	print("Touched")
	if other_part.Parent:FindFirstChildOfClass("Humanoid") then
		local PLYRS: Players = game:GetService("Players")
		local plyr: Player = PLYRS:GetPlayerFromCharacter(other_part.Parent)
		Collect:FireClient(plyr, part)
		task.wait(2)
	end
end

part.Touched:Connect(on_hit)

inside the part

will this work because the gems spawn on the client side and will only be detected on the client

1 Like

With the Collect event, you put it in the wrong terms. At the start of the topic, in your collect script, it says FireServer(). So I think what the problem here is, instead of putting Collect.OnServerEvent:Connect(function(player, part), you put Collect.OnClientEvent:Connect(function(player, part). It is a difference from the Server and the Client, since you are using a ServerScript, you would put Collect.OnServerEvent:Connect(function(player, part), not Collect.OnClientEvent:Connect(function(player, part).

nvm

The code below would be in a ServerScript:

local ServerStorage = game:GetService("ServerStorage")
local Debounce = false
local Collect = game:GetService("ReplicatedStorage").Collect

for _, eachcoin in pairs(workspace:GetChildren()) do
	if eachcoin:IsA("Part") and eachcoin.Name == 'coin' then
		coroutine.resume(coroutine.create(function()
			eachcoin.Touched:Connect(function(hit)
				local human = hit.Parent:FindFirstChild("Humanoid")
				if eachcoin ~= nil and (human ~= nil) and Debounce ~= true then
					Debounce = true
					local player = game.Players:GetPlayerFromCharacter(hit.Parent)
					print(player.Name)
					eachcoin:Destroy()
					Collect:FireClient(player,eachcoin)
					Debounce = false
				end
			end)
		end))
	end
end

The code below is in a LocalScript:

local Collect = game:GetService("ReplicatedStorage").Collect

local Configuration = {
	GEM_COLLECT = 1 * 2
}

local value = 0

Collect.OnClientEvent:Connect(function(player, part)
	value = value + Configuration.GEM_COLLECT
	print(value)
end)