This script is meant to detect if another trap is nearby, using magnitude, but it only works the first time, and then afterwards it just lets me place them however close to another trap
local function checkIfTrapsClose()
if #game.Workspace.GameAssets.Traps:GetChildren() > 0 then
for i, trap in pairs(game.Workspace.GameAssets.Traps:GetChildren()) do
if trap then
if (trap.HumanoidRootPart.Position - character.TrapPart.Position).Magnitude > 10 then
return true
else
return false
end
else
return false
end
end
else
return true
end
return false
end
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.LeftControl and checkIfTrapsClose() then
print(checkIfTrapsClose())
game.ReplicatedStorage.Events:WaitForChild("placeTrap"):FireServer()
end
end)
local function checkIfTrapsClose()
if #game.Workspace.GameAssets.Traps:GetChildren() > 0 then
for i, trap in pairs(game.Workspace.GameAssets.Traps:GetChildren()) do
if trap then
if (trap.HumanoidRootPart.Position - character.TrapPart.Position).Magnitude > 10 then
return true
else
return false
end
else
return false
end
end
else
return true
end
return false
end
UserInputService.InputBegan:Connect(function(gameProsseced,input)
if not gameProsseced then
if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.LeftControl and checkIfTrapsClose() then
print(checkIfTrapsClose())
game.ReplicatedStorage.Events:WaitForChild("placeTrap"):FireServer()
end
end
end)
What you’re doing in this code is only checking for one trap. Remember that returning will cease the execution of a function and return a value back to where the function was called.
local function ret_value()
return true
print("We will never make it here because we've already returned a value")
end
How I’d change up your checkIfTrapsClose() method is something like this:
local function checkIfTrapsClose()
local traps = workspace.GameAssets.Traps:GetChildren()
if #traps > 0 then
for _, trap in ipairs(traps) do -- ipairs because we iterating through an array of traps
if (trap.HumanoidRootPart.Position - character.TrapPart.Position).Magnitude < 10 then
return false -- return false if a trap is too close to our character (less than 10 studs away)
end
end
end
return true -- return true if there are no traps in the game OR no traps nearby
end
In addition, you might run into problems with this section of your code:
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.LeftControl and checkIfTrapsClose() then
print(checkIfTrapsClose())
game.ReplicatedStorage.Events:WaitForChild("placeTrap"):FireServer()
end
end)
Switch that to this:
UserInputService.InputBegan:Connect(function(input)
if (input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.LeftControl) and checkIfTrapsClose() then
print(checkIfTrapsClose())
game.ReplicatedStorage.Events:WaitForChild("placeTrap"):FireServer()
end
end)
What I changed between the first and the second is I added parenthesis to your input.KeyCode logic. In your original statement, your conditional was doing this:
If our player clicks LeftShift, enter the conditional OR if our player clicks left control AND checkIfTrapsClose() == true, enter the conditional.
The new if statement is doing this:
If our the player clicks either LeftShift OR left control AND checkIfTrapsClose() == true, enter the conditional.
If you need me to follow up on an explanation with that, feel free to ask.