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)
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)
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…