Can I make it so that when a player has a tool out they get double the amount of points when touching a part?

So I have been working on this trash picking simulator type game, i’ve made it so that when you pick up a piece of trash you get a point. You can trade out your points for money, then buy tools.

I want to make it so that when you hold out a tool you get 2 points instead of one.

local Player = game.Players.LocalPlayer
local plaayer = game.Players:GetPlayerFromCharacter()
local Character = Player.Character or Player.CharacterAdded:Wait()
Character.ChildAdded:Connect(function(NewChild)
	   if NewChild:IsA("Tool") then
   plaayer.leaderstats.Plastic.Value = plaayer.leaderstats.Plastic.Value *2 --a new tool is equipped
   end
end)
--then to detect if the tool is unequipped you do the opposite
Character.ChildRemoved:Connect(function(RemovedChild)
   if RemovedChild:IsA("Tool") then
   plaayer.leaderstats.Plastic.Value = plaayer.leaderstats.Plastic.Value *1 --the tool is unequipped
   --RemovedChild is the tool unequipped
   end
end)

This is the local script that detect when you have a tool out or not. How can I do this?

I’d like to assume you have Workspace.FilteringEnabled set to true for security reasons, but in this case, LocalScripts can’t modify leaderstats on the server.

Try creating a RemoteEvent and put it in the ReplicatedStorage to tell the server to change that value for the player:

-- Local script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickedUpTrashWithToolEvent = ReplicatedStorage:WaitForChild("PickedUpTrashWithToolEvent") -- Assuming you put the RemoteEvent in the RS

local Player = game.Players.LocalPlayer
local plaayer = game.Players:GetPlayerFromCharacter()
local Character = Player.Character or Player.CharacterAdded:Wait()

Character.ChildAdded:Connect(function(NewChild)
  if NewChild:IsA("Tool") then
    PickedUpTrashWithToolEvent:FireServer()
  end
end)

Also, put a server Script inside of the ServerScriptService:

-- Server script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickedUpTrashWithToolEvent = ReplicatedStorage:WaitForChild("PickedUpTrashWithToolEvent")

PickedUpTrashWithToolEvent.OnServerEvent:Connect(function(player)
  player.leaderstats.Plastic.Value = player.leaderstats.Plastic.Value + 2 -- Changed * to + because you said you wanted 2 points; not multiply their current Plastic by 2
end);
1 Like
local Player = game.Players.LocalPlayer
local plaayer = game.Players:GetPlayerFromCharacter()
local Character = Player.Character or Player.CharacterAdded:Wait()
Character.ChildAdded:Connect(function(NewChild)
	   if NewChild:IsA("Tool") then
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickedUpTrashWithToolEvent = ReplicatedStorage:WaitForChild("PickedUpTrashWithToolEvent") -- Assuming you put the RemoteEvent in the RS

Character.ChildAdded:Connect(function(NewChild)
  if NewChild:IsA("Tool") then
    PickedUpTrashWithToolEvent:FireServer()
			  	end
			end)
		end
	end)
--then to detect if the tool is unequipped you do the opposite
Character.ChildRemoved:Connect(function(RemovedChild)
   if RemovedChild:IsA("Tool") then
   plaayer.leaderstats.Plastic.Value = plaayer.leaderstats.Plastic.Value *1 --the tool is unequipped
   --RemovedChild is the tool unequipped
   		end
	end)

I’m sorry I started scripting last week, I don’t really understand…

1 Like

No prob! I’ll explain what I mean.

In your Character.ChildRemoved code, you also try to change the Plastic variable from the client side (the player’s view). Unfortunately, FilteringEnabled prevents your LocalScripts from changing values on the server, but the good side is that it stops exploiters from being able to change values without getting checked by the server.

You can use the same RemoteEvent to make this happen, but you can rename it to reuse it with another function:

-- Local script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickedUpTrash = ReplicatedStorage:WaitForChild("PickedUpTrash")

local Player = game.Players.LocalPlayer
local plaayer = game.Players:GetPlayerFromCharacter()
local Character = Player.Character or Player.CharacterAdded:Wait()

Character.ChildAdded:Connect(function(NewChild)
  if NewChild:IsA("Tool") then
    PickedUpTrash:FireServer(true) -- the tool is equipped
  end
end)

Character.ChildRemoved:Connect(function(RemovedChild)
   if RemovedChild:IsA("Tool") then
     PickedUpTrash:FireServer(false); -- the tool is unequipped
   end
end)

I removed the extra ends in your LocalScript, but you should add them back if you copied the code from a function.

-- Server script (the same one)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickedUpTrash = ReplicatedStorage:WaitForChild("PickedUpTrash")

PickedUpTrash.OnServerEvent:Connect(function(player, playerEquippedTool)
  if playerEquippedTool then
    player.leaderstats.Plastic.Value = player.leaderstats.Plastic.Value + 2 -- Changed * to + because you said you wanted 2 points; not multiply their current Plastic by 2
  else 
    player.leaderstats.Plastic.Value = player.leaderstats.Plastic.Value + 1 -- Changed * to + because you said you wanted 1 point; not multiply their current Plastic by 1
  end;
end);
1 Like

16:11:34.267 - Infinite yield possible on ‘ReplicatedStorage:WaitForChild(“PickedUpTrash”)’

Where do I place the local script…?

And I re named the remote event to “PickedUpTrashWithToolEvent” was I supposed to to that?

1 Like

You keep the LocalScript where you had it.

Also, you have to rename the RemoteEvent from PickedUpTrashWithToolEvent to PickedUpTrash.

1 Like

Thank you this helped a lot

but everytime you pull out the tool you get trash

1 Like

I had a feeling that would be a problem. Sounds like they’d get rich with plastic pretty fast :laughing:

You can solve that problem by just modifying a variable if the player has the tool out or not, and checking it when they collect trash.

1 Like
Character.ChildAdded:Connect(function(NewChild)
  if NewChild:IsA("Tool") then
		script.Parent.Parent.Workspace.Wrapper.Touched:Connect(function(hit)
			 PickedUpTrash:FireServer(true) -- the tool is equipped
		end)
  end
end)

Something like this?

local wappa = script.Parent
local dp = true
local pst = {Vector3.new(133, 3.5, 40), Vector3.new(-164, 3.5, 47), Vector3.new(-165, 3.5, 203)}
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if dp == true then 
			dp = false
			script.Parent.Transparency = 1
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Plastic.Value = player.leaderstats.Plastic.Value + 1
			script.Parent.Transparency = 1
			wait(5)
			dp = true
			math.randomseed(tick())
			local posi = pst[math.random(1, #pst)]
			wappa.Position = posi
			script.Parent.Transparency = 0
		end
	end
end)

this is my plastic script

I’m sorry this is the last thing I need help with then I can finally work on the finishing parts of the game.

Oops! I think it would be safer if you handle all of the Plastic handling on the server. I just realized that an exploiter might be able to abuse the RemoteEvent if they know the name of it. Here’s the new code:

-- Server script
local Players = game:GetService("Players");

script.Parent.Parent.Workspace.Wrapper.Touched:Connect(function(hit)

    -- Check if the hit came from the tool
    local Player = Players:GetPlayerFromCharacter(hit.Parent); -- Assuming hit is a player limb; returns nil if it isn't a player
    local PlayerHasTool = false;
    if hit.Parent:IsA("Tool") then
       Player = Players:GetPlayerFromCharacter(hit.Parent.Parent); -- Assuming that hit.Parent is the handle
       PlayerHasTool = true;
    elseif not Player then
       return;
    end;

    if PlayerHasTool then
      Player.leaderstats.Plastic.Value = player.leaderstats.Plastic.Value + 2;
    else 
      Player.leaderstats.Plastic.Value = player.leaderstats.Plastic.Value + 1;
    end;
end)

You can remove the RemoteEvent.

thank you, but what about the localscript in starterpack

I thought of an alternative, What if instead of having the tool have a script I make it so that the plastic checks if the player is holding the tool out while they touch it so that they get double the amount of points.

This would definitely make the plastic scripts incredibly long but at least it’s better than the feature not ever being in the game.