Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

RemoteEvents: Why is this happening?

Asked by 5 years ago
Edited 5 years ago

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

https://gyazo.com/68233bf7775ccbf17425345bf7a8d35a

0
why do you have OnServerEvent in a local script? theking48989987 2147 — 5y
0
My bad, that's in a ServerScript not a local script. Edited it. nicktooner 119 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

I'm BlackOrange and I will be helping you today!

Issues:

Your biggest issue is misunderstanding why we use RemoteEvents and also what we should do on the Client and on the Server.

  1. This issue is really big. You are constantly firing a remote to the server in a LOOP this basically spells death on your game

  2. You got some stuff in there that you shouldn't and there are better ways to do things.

Solution:

Alright, let's think. Do you really want the Server to put a selection box around a part? When the server does it, it's not only the player that is seeing it. The ENTIRE server including all the players can see it. This could be chaotic. What if all the players hovered over something, all of them would be selected from the Client. I don't see why you would do this in the first place.

Alright, so for this "Project" / "Task", you will NOT be needing a ServerScript. All you need is a LocalScript inside of StarterCharacterScripts in StarterPlayer

Let's take a look at wiki: https://www.robloxdev.com/api-reference/class/SelectionBox

So how would you be able to fix your problem?

Here are the steps:

  1. Remove the remote event

  2. Re write everything but this time do it on the client only

How would this be beneficial?

Well

  1. You aren't killing your game

  2. You aren't using a loop

  3. It would look much smoother on the client

So yea, do it on the client. Remember when you gotta do stuff and why.

Best of luck developer! hoped this helped!

0
Damn, one of the most helpful answers I've received. I'm still in the transition phase from Non FE to FE and for some reason I seem to think that everything requires a Remote Event/Function now, thanks for explaining it. nicktooner 119 — 5y
0
No problem BlackOrange3343 2676 — 5y
Ad
Log in to vote
1
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago

Part 1: How I would do it (skip this if you want the answer to your question) Hey, so instead of creating a new part for the selection box, let's use an instance called SelectionBox.

Let's start by creating this instance and placing it inside StarterGui.

Now that you've created the instance you might be wondering how I can connect it to a part, to do that you just have to set it's Adnornee to the part.

Using your client script we can do this.

play = game.Players.LocalPlayer
mouse = play:GetMouse()
local selectionbox = play.PlayerGui:WaitForChild("SelectionBox") --also gatta wait for things to load :p

if mouse.Target ~= nil then
    while wait() do
    if target.Name ~= "Baseplate" then
        selectionbox.Adornee=mouse.Target
    end
        wait()
    end
end

Part 2: Answer to your question

For the LocalScript we need to remove the check for mouse.Target BEFORE the actual loop starts. Once the loop starts we can check to see if mouse.Target is nil and the target's name is not "TargetPart".

play = game.Players.LocalPlayer
mouse = play:GetMouse()

while wait() do
    if mouse.Target and mouse.Target.Name~="TargetPart" then
        game.ReplicatedStorage.Target:FireServer(mouse.Target)
    end
    wait()
end

Let's destroy the -

SERVERSCRIPT For destroying Part, Parented to the Part. We only need to deal with the main server script now.

In the ServerScript we begin by setting curPart as a global variable.

.Next we can set curPart to be a clone of TargetPart and set that up and if target is nill or doesnt pass our first if statement, we can Destroy() curPart if it exists to remove it once you move your mouse off the part.

local curPart
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
        curPart = game.ReplicatedStorage.TargetPart:Clone()
        curPart.Size = target.Size + Vector3.new(0.2,0.2,0.2)
        curPart.Anchored = true
        curPart.CFrame = target.CFrame
        curPart.Parent = target
        print(target.Name)
    elseif curPart then
        curPart:Destroy()
    end
end)

Please ask questions if you have any.

0
Thanks, both your answers helped a lot, unfortunately I can only accept one :/ nicktooner 119 — 5y

Answer this question