What do you want to achieve?
I need help with fixing this local script in a image button
What is the issue?
attemp to index nil with destroy, the deadbody
3.More Details
My script is finding a Union ( Dead Body ) in workspace and if the Union is close to the player enough you can click the button to report it.
--This Is My Script
local MaxRange = 5
local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local Debounce = false
local function findTarget()
local target = nil
for i,v in pairs(game.Workspace:GetChildren()) do
local body = v
if body.Name == "DeadBody" and v ~= Player.Character then
if (Player.Character:FindFirstChild("HumanoidRootPart").Position - v.Position).magnitude < MaxRange then
target = body
end
end
end
return target
end
while wait() do
local deadBody = findTarget()
if deadBody then
if not Player.Character:FindFirstChild("Ghost") then
script.Parent.ImageColor3 = Color3.fromRGB(255, 255, 255)
script.Parent.MouseButton1Click:Connect(function()
if Debounce == false then
Debounce = true
script.Report:FireServer(deadBody)
deadBody:Destroy()
wait(3)
deadBody = findTarget()
Debounce = false
end
end)
end
else
script.Parent.ImageColor3 = Color3.fromRGB(75, 75, 75)
deadBody = findTarget()
end
end
I think the problem is with how you’re using :Connect() inside of the while loop without disconnecting it
once findTarget() doesn’t return nil for the first time the loop will go into the if statement to show the report button and setup MouseButton1Click:Connect()
this makes it so whenever you click the button, the function will trigger as intended
PROBLEM1: but the problem is on the next iterations of the loop, until you click the button, the loop will setup MouseButton1Click:Connect(), so once you click the button, it’ll trigger each of those connections at the same time. atleast of the connections will trigger the debounce, fire to the server, and destroy the body, if more than one trigger it, you’ll have the nil error
PROBLEM2: then, after the debounce ends, you still didn’t disconnect the connection, so clicking the button again will trigger all of those old connections and any new ones, if an old one triggers, it’ll try to destroy the old dead body that’s already destroyed
Sorry for making you to type this much lol but this was what i did
local maxRange = 5
local Debounce = false
local player = game.Players.LocalPlayer
function findDeadBody()
local target = nil
for i,v in pairs(workspace:GetChildren()) do
if v.Name == "DeadBody" and v ~= player.Character then
if (player.Character:FindFirstChild("HumanoidRootPart").Position - v.Position).magnitude < maxRange then
target = v
Debounce = false
end
end
end
return target
end
while wait() do
local deadBody = findDeadBody()
if deadBody and not player.Character:FindFirstChild("Ghost") then
script.Parent.ImageColor3 = Color3.fromRGB(255, 255, 255)
script.Parent.MouseButton1Click:Connect(function()
if Debounce == false then
Debounce = true
script.Report:FireServer(deadBody)
wait(1)
Debounce = false
end
end)
else
Debounce = true
script.Parent.ImageColor3 = Color3.fromRGB(75, 75, 75)
end
end