How would I make it so When The target changes it fires an event that let’s the server know what target
the player pointed at , but the event might also fire when the player has clicked (I already have the script for this no worry) But in the server script onserver function it has a while loop how would I change the target variable when the target changes in the function because it doesn’t change the variable while it is already running
also don’t worry I am not going to make it so it fires a server event when the mouse target changes it is just a example
game:GetService("RunService").RenderStepped:Connect(function()
local mouse = game.Players.LocalPlayer:GetMouse()
target = mouse.Target
if target then
if Player:DistanceFromCharacter(target.Position) < 15 then
SelectionBox.Adornee = target
else
SelectionBox.Adornee = nil
end
if not LastTarget then
LastTarget = target
end
if target ~= LastTarget then
EventActivate:FireServer(target,MouseHeld,tool)
LastTarget = nil
end
end
end)
So you want to check on the server if the returned Target is different than the last one? If so, you can do something like this:
local RS = game:GetService("ReplicatedStorage")
local event: RemoteEvent
local existing = {}
event.OnServerEvent:Connect(function(player: Player, target: BasePart)
if existing[player] then
if existing[player] == target then
warn("nothing changed")
else
warn("omgggg it changed!!")
end
end
existing[player] = target
end)
local playerInstance = game:GetService("Players").LocalPlayer
local playerMouse = playerInstance:GetMouse()
local remoteEvent = game.ReplicatedStorage.RemoteEvent
playerMouse.Move:Connect(function()
local target = playerMouse.Target
local function check(lastTarget)
if tostring(target) ~= lastTarget then
remoteEvent:FireServer()
end
end
local lastTarget = script.LastTarget
check(lastTarget.Value)
lastTarget.Value = tostring(target)
end)
Make sure it’s a LocalScript and it has a StringValue with the name “LastTarget” as a child.
Not sure if it’s what you needed, but this is what I used for my game and it worked. If you need to, you can change some things in the Script to make it detect whenever someone clicked.
local Game = game
local RunService = Game:GetService("RunService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local OldTarget = nil
local function OnRenderStep()
local NewTarget = Mouse.Target
if NewTarget == OldTarget then return end
print(NewTarget.Name)
OldTarget = NewTarget
end
RunService.RenderStepped:Connect(OnRenderStep)