Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a script give players money with a click detector?

Okay, so I was trying to make a script that will give players money in game when they click a button, however I don't know how. Could someone help me? Here's my script.

local Ss = game:GetService("ServerStorage")
local Rs = game:GetService("ReplicatedStorage")
local ws = game:GetService("Workspace")
local plrs = game:GetService("Players")
local db = true
local dialog = Rs:WaitForChild("Dialog")

script.Parent.MouseClick:Connect(function()
	if db == true then
	db = false
	script.Parent.Parent.Size = Vector3.new(0.8, 0.08, 1.21)
	script.Parent.Parent.Position = Vector3.new(3.86, 20.546, 12.427)
	local events = Ss:WaitForChild("Events")
	local availableEvents = events:GetChildren()
	local chosenEvent = availableEvents[math.random(1,#availableEvents)]
	local clonedEvent = chosenEvent:Clone()
	print("Spawning "..clonedEvent.Name.."...")
	script.Parent.MaxActivationDistance = 0
	clonedEvent.Parent = workspace
	script.Parent.Parent.Material = Enum.Material.SmoothPlastic
	script.Parent.Parent.Color = Color3.fromRGB(255,0,0)
	script.Parent.Parent:WaitForChild("Minecraft Sounds: Piston Out"):Play()
	dialog.Value = clonedEvent.Name.."!"
	wait(10)
	script.Parent.Parent.Size = Vector3.new(0.8, 0.24, 1.21)
	print("It's been 10 seconds, so destroying "..clonedEvent.Name.."...")
	clonedEvent:Destroy()
	script.Parent.Parent.Material = Enum.Material.Neon
	script.Parent.Parent.Position = Vector3.new(3.86, 20.59, 12.472)
	script.Parent.Parent.Color = Color3.fromRGB(0,255,0)
	script.Parent.Parent:WaitForChild("Minecraft Sounds: Piston Out"):Play()
	script.Parent.MaxActivationDistance = 32
	dialog.Value = "No event!"
	db = true
end
end)

[SerpentineKing]: Added Code Block

2 answers

✓ Accepted answer

i did this before and also this is not a request site but i will answer it

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	cash = player.leaderstats.Cash
	cash.Value = cash.Value + 500 -- change 500 to what ever number you want
end)

Improvements

Use Local Variables for instances which are used multiple times in your script

Information

The MouseClick event has a parameter for the player who activated the ClickDetector

Line 17 contains the instance for the coins, Line 32 contains the change in coin value

I added spaces in the code to separate different events occurring once the function is run

Revised Server Script

local SS = game:GetService("ServerStorage")
local events = SS:WaitForChild("Events")

local repst = game:GetService("ReplicatedStorage")
local dialog = repst:WaitForChild("Dialog")

local detect = script.Parent
local part = detect.Parent
local audio = part:WaitForChild("Minecraft Sounds: Piston Out")

local db = true

detect.MouseClick:Connect(function(player)
	if db == true then
		db = false
		
		local coins = player:WaitForChild("leaderstats"):WaitForChild("Coins")
		local availableEvents = events:GetChildren()
		local chosenEvent = availableEvents[math.random(1, #availableEvents)]
		local clonedEvent = chosenEvent:Clone()
		clonedEvent.Parent = workspace
		print("Spawning "..clonedEvent.Name.."...")
		
		part.Size = Vector3.new(0.8, 0.08, 1.21)
		part.Position = Vector3.new(3.86, 20.546, 12.427)
		part.Material = Enum.Material.SmoothPlastic
		part.Color = Color3.fromRGB(255, 0, 0)
		audio:Play()
	    
		detect.MaxActivationDistance = 0
		dialog.Value = clonedEvent.Name.."!"
		coins.Value = coins.Value + 10
		wait(10)
		
		print("It's been 10 seconds, so destroying "..clonedEvent.Name.."...")
		part.Size = Vector3.new(0.8, 0.24, 1.21)
		clonedEvent:Destroy()
		
		part.Material = Enum.Material.Neon
		part.Position = Vector3.new(3.86, 20.59, 12.472)
		part.Color = Color3.fromRGB(0, 255, 0)
		audio:Play()
		
		detect.MaxActivationDistance = 32
		dialog.Value = "No event!"
		
		db = true
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.