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 it so that a player gets points when they click a part with a tool out?

I'm currently making a game with a job that requires you to mop floors with a sponge, I have no idea how to make it so that when you click a dirty part with a sponge, it gives you points and the part's transparency is set to 1, I tried looking online but nothing helped. Here's some video footage of the almost finished job so that it's easier for you to help me. (https://streamable.com/kgedln)

I tried doing it in a localscript (parent is the tool (SPONGE)) but then just gave up

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Dirt1 = game.Workspace.DirtStains.Dirt1
local Dirt2 = game.Workspace.DirtStains.Dirt2
local Dirt3 = game.Workspace.DirtStains.Dirt3
local Dirt4 = game.Workspace.DirtStains.Dirt4

Mouse.Idle:Connect(function()
	if Mouse.Target == Dirt1 or Dirt2 or Dirt3 or Dirt4 then
		game.Players.LocalPlayer.leaderstats.Money.Value + 15
	end
end)

Thank you!

2 answers

✓ Accepted answer

Alright, so this is personally how I would do this (put a remote event in ReplicatedStorage named RemoteEvent btw) here's the script (explanations are inside the script)

-- local script
-- put this script inside your tool
-- this is important - if you have more than one part to make one dirt stain, group all of your parts and name the group DirtStain (doesn't matter what the children of the group are called)
-- also it doesn't matter if you only have one part to make one dirtstain, just group it by itself and name it DirtStain for it to work

local RPS = game:GetService("ReplicatedStorage") -- getting ReplicatedStorage
local RemoteEvent = RPS:WaitForChild("RemoteEvent") -- defining the remote event (change the name of it if needed)
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Equipped = false
local Tool = script.Parent
local DirtStains = game.Workspace.DirtStains -- folder that holds dirt stains

Mouse.Button1Down:Connect(function()
	if Equipped then -- if tool is equipped
		if Mouse.Target.Parent.Name == "DirtStain" then -- if group of target is named dirtstain then
			RemoteEvent:FireServer(Mouse.Target.Parent) -- firing a server event that sends the information of the dirt stain
		end
	end
end)

Tool.Equipped:Connect(function() -- function to check if tool is equipped
	Equipped = true
	print("Equipped")
end)

Tool.Unequipped:Connect(function() -- function to check if tool is not equipped
	Equipped = false
	print("Unequipped")
end)

and here is the server script, put this inside ServerScriptStorage:

-- put this script inside ServerScriptStorage

local RPS = game:GetService("ReplicatedStorage") -- getting ReplicatedStorage
local RemoteEvent = RPS:WaitForChild("RemoteEvent")
local Time = 5 -- how long it will take for the DirtStain to respawn

RemoteEvent.OnServerEvent:Connect(function(Player, DirtStain)
	for i,v in pairs(DirtStain:GetChildren()) do -- getting children of the group
		v.Transparency = 1 -- makes all children of the group completely transparent
	end
	local Points = Player.leaderstats:WaitForChild("Points")
	if DirtStain.Name == "DirtStain" then
		DirtStain.Name = "Clean" -- changing "DirtStain" to "Clean" so you can't spam collect points
		Points.Value = Points.Value + 1 -- gives you a point
		wait(Time)
		
		-- IF YOU WANT THE DIRTSTAIN TO RESPAWN KEEP THIS, OTHERWISE DELETE THIS VVV
		for i,v in pairs(DirtStain:GetChildren()) do -- getting children of the group
			v.Transparency = 0 -- makes all children of the group opaque
		end
		DirtStain.Name = "DirtStain"
		-- IF YOU WANT THE DIRTSTAIN TO RESPAWN KEEP THIS, OTHERWISE DELETE THIS ^^^
	end
end)

Here is the final product: https://streamable.com/mpca4t

I didn't make leaderstats so I used print to show how many points I would have

If you have any questions please ask me ASAP so I can answer them, if you found this helpful please accept my answer.

2 comments

0

Yes, thank you! Exactly what I needed, I never though someone would put so much effort into answering a question like this, you're awesome!

0

No problem!

First Of All, We need to know if the player actually cleans up the mess, So after you clean it up an event will fire and if that event is fired then in a server script the value of the points increases, and then define the variable of dirt in the script and set the transparency to 1. Please upvote this if it works. Here are the scripts for if you're confused.

--Code here
Animation:Play()
local event = Instance.new("RemoteEvent", game.ReplicatedStorage)
event.Name = "CleanedUp"
event:FireServer(game.Players.LocalPlayer)
--Code here
game.ReplicatedStorage:WaitForChild("CleanedUp").OnServerEvent:Connect(function(Player)
       local points = Player.leaderstats:WaitForChild("Points")
       points.Value = points.Value + 1
       local Dirt = DirtVariable
       Dirt.Transparency = 1
end

1 comment

0

I tried modifying the script in every possible way, either it gave me an error or just didnt work and when I removed all extras and just copy and pasted your code (with slight modifications for it to work properly), it again just didn't work, I previously successfully made a script that works just like I wanted it to but it was on the client so I couldn't actually buy anything and needed a RemoteE

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