Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I made a script that deletes the mouse target, why is my player being removed?

I am using filtering enabled, so I had to use remote events. Though when I did it without remote events it worked fine.

Also it's not just the character that's being removed, the player is being removed from the game.

Local Script

local selectionBox = Instance.new("SelectionBox")


local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

selectionBox.Parent = player:WaitForChild('PlayerGui')

local destroyPart = game.ReplicatedStorage.DestroyPart

mouse.Move:Connect(function()
	local target = mouse.Target
	
	if not target then return end
	
	if target and target:IsA("BasePart") then
		selectionBox.Adornee = target
	else
		selectionBox.Adornee = nil
	end
end)

mouse.Button1Down:Connect(function()
	local target = mouse.Target
	
	if not target then return end
	
	if target and target:IsA("BasePart") and selectionBox.Adornee and selectionBox.Adornee:IsA("BasePart") and not target:IsDescendantOf(player) and not target:IsDescendantOf(player.Character) then
		print(target)
		destroyPart:FireServer(target)
		selectionBox.Adornee = nil
	end
	
end)

Server Script:

local destroyPart = game.ReplicatedStorage.DestroyPart

destroyPart.OnServerEvent:Connect(function(part)
	if not game.Players:GetPlayerFromCharacter(part.Parent) then
		part:Destroy()
	end
end)

1 answer

✓ Accepted answer

The first parameter of the event listener function for OnServerEvent is the player from which FireServer was called. So, part in your OnServerEvent function isn't the part; it's the player. This can be easily remedied by just adding in the player parameter:

local destroyPart = game.ReplicatedStorage.DestroyPart

destroyPart.OnServerEvent:Connect(function(player, part)
    if not game.Players:GetPlayerFromCharacter(part.Parent) then
        part:Destroy()
    end
end)

Hope this helped.

1 comment

0

Thanks, I forgot all about the first parameter being the player.

This discussion is preserved as it was. New answers, comments, and votes are closed.