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

Remote Event to destroy block instead destroys my player model?

I have a script that whenever you click will check the item name and send a Remote Event to destroy the block, the event goes through and upon clicking will destroy, but instead of destroying the target it destroys my player's Workspace model.

Local Script located in StarterPlayerScripts:

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

mouse.Button1Down:Connect(function()
    if mouse.Target.Name == "Item" or "Item2" then
        target = mouse.Target
		game.ReplicatedStorage.BlockDestroy:FireServer(target)
    end
end)

(Theres a Remote event in ReplicatedStorage called "BlockDestroy".)

Script in Workspace to handle the Remote Event:

game.ReplicatedStorage.BlockDestroy.OnServerEvent:Connect(function(target)
	target:Destroy()
end)

1 answer

In this line, you're using the 'or' operator incorrectly

if mouse.Target.Name == "Item" or "Item2" then

This is partitioned as "if the target's name is 'Item', or if the string 'Item2' exists, move on". You need another '==' operator, like so.

if mouse.Target.Name == "Item" or mouse.Target.Name =="Item2" then

If you want to avoid confusion, you can change the priority of the statements manually with parentheses, like so

if (mouse.Target.Name == "Item") or (mouse.Target.Name =="Item2" then)

Here's a good reference for lua operators: https://www.tutorialspoint.com/lua/lua_operators.htm

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