Touched event is not firing

Hello there. I am beginner scripter, and i want to make that on touch to a part, player gets +1 to an specific value.

The problem is when i try to touch the part, onTouch event is not firing. I tried to make it unAnchored, but that have done no progress.

Here is my code for the part:

local cash = game.ReplicatedStorage.cash
local valueToGive = 1

cash.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			local leaderstats = player.leaderstats
			local cash = leaderstats.Cash
			print("gave to "..player.Name .." ".. valueToGive .. " cash")
			cash.Value = cash.Value + 1
		end
		cash:Destroy()
	end
end)

Any help will be appreciated. :blush:

3 Likes

Where is your script located? Is it a local script or a server script?

By the way, make sure the CanTouch property is set to true for that touch part. Any parts that have it set to false will be ignored by the .Touched event.

Debug your code as well. It can be done with print statements. Run this code and let me know of the results:

print("Script is running.")
local cash = game.ReplicatedStorage.cash
local valueToGive = 1

cash.Touched:Connect(function(hit)
    print("Cash was touched.")
    if hit.Parent:FindFirstChild("Humanoid") then
        print("Humanoid was found.")
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
            print("Player was found.")
			local leaderstats = player.leaderstats
			local cash = leaderstats.Cash
			print("gave to "..player.Name .." ".. valueToGive .. " cash")
			cash.Value = cash.Value + 1
		end
		cash:Destroy()
	end
end)
2 Likes

This script has to fire when it’s out of ReplicatedStorage. It cant fire while in ReplicatedStorage, Though it can in Workspace.

That as well, but he certainly tried to touch it so it was in workspace.

1 Like

The part has to be in workspace right when the script fires. So, you can do a loop to find the part, if it’s found then fire the script.

If my suggestion does not work, try anchoring it.

1 Like

Script is located in cash part. The script is a serverscript.

And yes, i made sure that CanTouch property is set to true for that touch part.
And when i debugged it, nothing in output was shown.

1 Like

That’s why, Put the script in workspace. Then access every parent again.

1 Like

Yes, but i am trying also to spawn cash in workspace by clicking to a certain part. Cash is spawning, but touch event is not firing.

1 Like

If needed, there is a code for spawning cash:

local deb = true
local sounds = script.Parent.Parent.Parent.Sounds:GetChildren()

script.Parent.MouseClick:Connect(function(plr)
	if deb == true then
		game.ReplicatedStorage.Events.SpawnCash:FireAllClients()
		local toplay = sounds[math.random(1,#sounds)]
		toplay:Play()
		deb = false
		wait(0.4)
		deb = true
	end
end)

You connect the .Touched event to each cash part. You have to loop through them and then connect it.

Example:

local CashParts = --can be a folder or collectionservice tag collection

for _, cashpart in ipairs(CashParts:GetChildren()) do -- we iterate through every cash part
    cashpart.Touched:Connect(function(hit) -- connect the touched event to the part

    end)
end

And no, the for loop will not yield in any way.

im so but you are making it so replicatedstorage cash part would only work on touched bu not the cloned cash.

1 Like

you might need to do this

game.Workspace.DescendantAdded:Connect(function(thing)
if thing.Name == "cash" and thing:IsA("BasePart") then
local cash = thing
local valueToGive = 1

cash.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			local leaderstats = player.leaderstats
			local cash = leaderstats.Cash
			print("gave to "..player.Name .." ".. valueToGive .. " cash")
			cash.Value = cash.Value + 1
		end
		cash:Destroy()
	end
end
end)
end)

ignore my british grammar

New Script:

local cash = game.ReplicatedStorage.cash
local valueToGive = 1

cash.Touched:Connect(function(hit)
    local h = hit
    if h then
       print("Touched "..cash.Name)
       if hit.Parent:FindFirstChild("Humanoid") then
	   local character = hit.Parent.Character
       local player = game.Players:GetPlayerFromCharacter(character)
       if player then
          local leaderstats = player.leaderstats
          local cash = leaderstats.Cash
          print("gave to "..player.Name .." ".. valueToGive .. " cash")
          cash.Value = cash.Value + 1
       end
       cash:Destroy()
    end
end)

I changed other little things in the script too.

This is not a solution. He is cloning the cash part. The .Touched event will not connect to the cloned cash parts. He needs to connect it for every one of the cash parts.

Nope. And thats not worked. Here is a code i wrote:

print("Script is running.")
local cash = workspace.CashFolder
local valueToGive = 1

cash.Touched:Connect(function(hit)
	for _, v in ipairs(cash:GetChildren()) do
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local character = hit.Parent
				local player = game.Players:GetPlayerFromCharacter(character)
				if player then
					print("Found a player named " .. player.Name)
					local ls = player.leaderstats
					local cash = ls.Cash
					cash.Value += valueToGive
					print("gave to " ..player.Name .. " " ..valueToGive .. "cash")
				end
			end
		end)
	end
end)

Hope thats help.

You’re detecting touch for a folder, looping through the folder once the folder is touched for no reason and then connecting the touched event again. You should do something similar to what @pumpkyrofl provided you but instead, you have a folder for the cash parts and you detect when an instance is added into that folder, then connect the event:

local FolderOfCashParts = -- reference here

FolderOfCashParts.ChildAdded:Connect(function(child)
    child.Touched:Connect(function(hit)

    end)
end)

Nope. Thats not working too as well.

I can’t understand. Am i missing something or what?
Still not working.

The code is:

local FolderOfCashParts = workspace.CashFolder:GetChildren() -- reference here
local valuetogive = 1
	FolderOfCashParts.ChildAdded:Connect(function(child)
		child.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local char = hit.Parent
			local player = game.Players:GetPlayerFromCharacter(char)
			if player then
				print("found a player")
				local ls = player.leaderstats
				local cash = ls.Cash
				cash.Value += valuetogive
			end
		end
		end)
	end)

Remove the :GetChildren() from the reference of the folder. You are detecting if a child is added into an array which is not possible. :GetChildren() returns an array of the children.