Trying to make a script that gives money to a certain player using a RemoteEvent

Basically, a player clicks a button, it clones an object with a string value of his player name. When it touches another part, it fires the event that gives money to the player. The event is supposed to fire when it touches the part, but it fires as soon as I spawn, and I don’t receive the money

Button script:

local btn = script.Parent
local prompt = btn.ProximityPrompt
local delay = btn.Delay.Value
local selectedObject = game.ReplicatedStorage.SelectedObject

prompt.Triggered:Connect(function(player)
	print("Clicked")
	for i,v in pairs(selectedObject:GetChildren()) do
		local clone = v:Clone() --clone it
		clone.Parent = game.Workspace --change the parent
		clone.Name = "Clone"
		clone:FindFirstChild("PlayerName").Value = player.Name
	end
	btn.Color = Color3.new(1, 0, 0)
	prompt.Enabled = false
	wait(delay)
	prompt.Enabled = true
	btn.Color = Color3.new(0, 1, 0)
end)

RemoteEvent script:

local GiveMoney = Instance.new("RemoteEvent")
GiveMoney.Name = "GiveMoney"
GiveMoney.Parent = game.ReplicatedStorage
local function Money()
	print("Server fired")
	local Clone = game.Workspace:FindFirstChild("Clone")
	if Clone then
		Clone.Destroying:Connect(function()
			local PlayerName = Clone.FindFirstChild("PlayerName").Value
			local ObjValue = Clone:FindFirstChild("Value").Value
			game.Players:FindFirstChild(PlayerName).leaderstats.Money.Value += ObjValue
		end)
	end
end
GiveMoney.OnServerEvent:Connect(Money())

FireServer() script:

local GiveMoney = game.ReplicatedStorage:WaitForChild("GiveMoney")

script.Parent.Touched:Connect(function(obj)
	if(obj.Name == "Clone") then
		print("Object is a Clone")
		GiveMoney:FireServer()
	end
end)

Add a print statement to check if the Clone object is being destroyed at all.

You we’re right. The clone doesn’t get destroyed, but the objects inside of it does. I fixed the issue and it works perfectly now. Thanks!

1 Like

The new problem is, the first clone gives you money, then when you do it again it doesn’t work. Do you know how I could fix that?

This is your piece of code. Change this part of the code so the code changes the name of the clone BEFORE changing its parent.
EDIT: I have just realized that this might not be the solution but try it anyway.

It sadly didn’t fix my issue. Thanks anyway.

If I’m not wrong you should put “:” not “.” before FindFirstChild.

Yeah I already fixed that issue

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