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