When I punch the level one punching bag, the script adds them all up because I have four models, each of which has an integer called Increase, making it difficult for me to distinguish which increment is which. How can I change this so that I only take a single item from the loop?
repStorage = game:GetService('ReplicatedStorage')
local Remotes = repStorage:WaitForChild('Remotes')
for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA('Model') then
if v:FindFirstChild('Increase') then
local Increase = v:WaitForChild('Increase')
Remotes.Punched.OnServerEvent:Connect(function(player)
player:WaitForChild("leaderstats").Punches.Value = player:WaitForChild("leaderstats").Punches.Value + Increase.Value
end)
end
end
end
You’re creating 4 listeners for your RemoteEvent, which is bad practice.
Create only one listener and wait for client’s data. Client can send you what punching bag is punched.
You didn’t provide much, so I can’t really tell you the best solution for this, but this whole code is definitely wrong.
Use something like
Remotes.Punched.OnServerEvent:Connect(function(player, bag)
local increase = bag:FindFirstChild("Increase")
local leaderstats = player:WaitForChild("leaderstats")
leaderstats.Punches.Value = leaderstats.Punches.Value + increase.Value
end)
You can distinguish different bags on client for example by Touched event, or if your player clicks on bag to hit it. Client best knows what bag is hit. Of course this will give some space for exploiters to exploit your game, but first you need to understand how it works.
local player = game:GetService('Players').LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild('Humanoid')
local mouse = player:GetMouse()
local repStorage = game:GetService('ReplicatedStorage')
local Remotes = repStorage:WaitForChild('Remotes')
local folder = workspace:WaitForChild('PunchingBags') -- Replace 'PunchingBags' with the actual name of the folder containing the punching bags
local debounce = false -- Create a debounce variable to track if the player can punch
local connection
local originalPosition = camera.CFrame.Position
for i, v in pairs(folder:GetChildren()) do
if v:IsA('Model') then
local Hitbox = v:WaitForChild('Hitbox')
Hitbox.Touched:Connect(function(touch)
local humanoid = touch.Parent:FindFirstChildOfClass('Humanoid')
if humanoid then
connection = mouse.Button1Down:Connect(function()
if not debounce then
debounce = true
Remotes.Punched:FireServer()
wait(0.15) -- Adjust the duration based on your desired debounce time
debounce = false
end
end)
Hitbox.TouchEnded:Connect(function()
connection:Disconnect()
end)
end
end)
end
end
In this case, you need to check which of the punchbags the player is touching, you can use workspace:GetPartsInPart().
local Players = game:GetService("Players")
Remotes.Punched.OnServerEvent:Connect(function(player)
for _, part in ipairs(workspace:GetPartsInPart(v.Hitbox)) do
if part.Parent:FindFirstChild("Humanoid") then
local otherPlayer = Players:GetPlayerFromCharacter(part.Parent)
if otherPlayer and otherPlayer == player then
player:WaitForChild("leaderstats").Punches.Value = player:WaitForChild("leaderstats").Punches.Value + Increase.Value
break
end
end
end
end)