Script is destroying random object instead of a certain object

So I’ve been stuck for the last few days working on a prop damage system for a private project and it’s been a headache. I got it to work, problem is that it randomly destroys any object named “test” instead of the certain “test” that was Touched by the player.
server

SPS_Limit = 25

function onTouched(part)
	--if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
	local GotSpeed = math.floor(part.Velocity.Magnitude/2)
	if part and part.Parent and GotSpeed > SPS_Limit then
		local Player = game.Players:GetPlayerFromCharacter(part.Parent)
		game.ReplicatedStorage.PartTouchedEvent:FireClient(Player)
		script.Parent.HitSound:Play()
	end
end

script.Parent.Touched:connect(onTouched)

localserver

local part = game.ReplicatedStorage.PartTouchedEvent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

part.OnClientEvent:Connect(function(otherPart)
	local remoteEvent = game:GetService("ReplicatedStorage").PartTouchedEvent
	remoteEvent:FireServer(Player, part, otherPart)
	for _, otherPart in pairs(game.Workspace:GetDescendants()) do
		if otherPart.Name == "test" and otherPart.Touched then
			otherPart.Anchored = false
			wait(2)
			otherPart:Destroy()
		end
	end
end)
1 Like

Could you just sent the test part in the partTouchedEvent in the server script? Also why are you trying to check if otherPart (which is the player) named “test”?

Because you are giving otherPart a new value, within the for loop your using a Variable that is the exact same name as the Parameter, the newest value would be given. And that said value, is the value of an index, within the table.

1 Like

Ok I get it but instead of looping through descendant’s in the server script you should pass script.parent through partTouchedEvent to get the part the player touched.

I’m confused, what do ya mean by “value of an index”?

It means that after OtherPart meaning player being the parameter of the function it’s the v in the for loop

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.