Touched event is not firing

Hello there. I want to make a part, that will give you +1 to cash when you touched it. Part is containing in ReplicatedStorage, and part spawns when clicked to an specific part.

The problem is when i instert part in workspace, and pre-loading it, touched event is firing fine. But when i trying to fire onTouch event on a spawned part, it does not fire. I tried to set it property CanTouch to true, in a script, and in properties menu. And i also tried this with CanCollide property.

Here is the code for the touching event:

local PlayerDataHandler = require(game.ServerScriptService:WaitForChild("PlayerDataHandler"))
local valueToGive = 1

cash.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			PlayerDataHandler:Update(player, "Cash", function(currentCash)
				return currentCash + valueToGive
			end)
		end
	end
end)

And here is the code for spawning the part:

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)

And the last code, RemoteEvent code:

local rs = game.ReplicatedStorage
local eventFolder = rs:WaitForChild("Events")

eventFolder.SpawnCash.OnClientEvent:Connect(function()
	print("worked")
	local cash = rs.cash:Clone()
	local cubeposition = workspace.cube.PrimaryPart.Position
	local cashPosition = cubeposition + Vector3.new(0, 3, 0)
	cash.Position = cashPosition
	cash.Parent = workspace
	cash.CanTouch = true
	cash.CanCollide = true
end)

Any help will be very appreciated! :slightly_smiling_face:

You’re spawned a part that’s local (from a local script) with a script in it.
If you check with the server-sided client you’ll see that the part does not show up.
Here are some things that should work:

  • Firing a new remote event in order to give the player its reward (Note: You can use the same remote event, if you want to.)
local PlayerDataHandler = require(game.ServerScriptService:WaitForChild("PlayerDataHandler"))
local valueToGive = 1

game.ReplicatedStorage.NEWREMOTEEVENT.OnServerEvent:Connect(function(player)
	if player then
		PlayerDataHandler:Update(player, "Cash", function(currentCash)
			return currentCash + valueToGive
		end)
	end
end)
cash.Touched:Connect(function()
    game.ReplicatedStorage.NEWREMOTEEVENT:FireServer()
end)
  • Spawning the part with a server-sided script, if you also want every single player in the game to be able to interact with the collectible cash.

Sorry if my English’s bad.
If my script does not work, please mention @jygttgyy, I’m not the best at scripting even tho I know a bit about it.

Sorry but that didn’t worked, but anyways, thanks you for helping!

Also, your english is not bad, i am learning it too, lol

No problem, sorry that I couldn’t help you out with this.

It should be NEWREMOTEEVENT:FireServer()

Bruh, what a stupid typo xD
I really suck.

Here also, edit your script and it should work.

No, i corrected it by myself already while i was typing it.

Just a heads up, because I’m pretty sure that sounds can only be played locally.
For example: Having a sound inside of a sound group inside of sound service, and then to play that through a local script…

This is playing through a serverscript.