I'm trying to learn remote events etc and I thought the best way to go about that would be to try create random things and manipulate them with remote events. I think I've made a-lot of progress so far but I've run into an issue. Essentially I'm trying to create a selection box around any part that the player is currently hovering over with their mouse, it works for the most part.
GIF of the issue https://gyazo.com/6ac440a161ab9a233b13e759903c8727
LOCALSCRIPT
play = game.Players.LocalPlayer
mouse = play:GetMouse()
if mouse.Target ~= nil then
while wait() do
game.ReplicatedStorage.Target:FireServer(mouse.Target)
wait()
end
end
SERVERSCRIPT
game.ReplicatedStorage.Target.OnServerEvent:Connect(function(player, target)
if target == nil then return end
if not target:FindFirstChild("TargetPart") and target.Name ~= "TargetPart" and target.Name ~= "Baseplate" then
local part = game.ReplicatedStorage.TargetPart:Clone()
part.Parent = target
part.Size = target.Size + Vector3.new(0.2,0.2,0.2)
part.Anchored = true
part.CFrame = target.CFrame
print(target.Name)
end
end)
The below script is to delete the part whenever the player removes their mouse from the part, I'm pretty sure this is where the problem is occurring, chances are that I just overlooked something really simple.
SERVERSCRIPT For destroying Part, Parented to the Part.
game.ReplicatedStorage.Target.OnServerEvent:Connect(function(player, target)
wait(target == nil)
script.Parent:Destroy()
end)
EDIT: So thanks to your guys help I've fixed the issue and improved it greatly, what took up 3 scripts before now only covers 14 lines.
play = game.Players.LocalPlayer
mouse = play:GetMouse()
local selectionbox = Instance.new("SelectionBox")
selectionbox.Parent = play.PlayerGui
mouse.Move:connect(function()
if mouse.Target == nil then end
if mouse.Target ~= nil then
if mouse.Target.Name == "Baseplate" then end
selectionbox.Adornee = mouse.Target
wait()
end
end)
If you're curious of what I was using it for then here's a GIF

2 comments
why do you have OnServerEvent in a local script?
My bad, that's in a ServerScript not a local script. Edited it.