So, I am trying to create a build system which includes a max distance. I want a max distance from the player’s mouse to the humanoid root part.
So I was trying to test it with using the print function.
I don’t understand the usage of the for loop in this script. This is what I mean:
for i = 2, 2 do
print(i) --The loop would run only once at it would print once value 2 and then stop
end
I would only do something like this for testing purposes, not for final product (this is not very likely).
Do you have any issues with the script? Does the script produce errors?
Player’s character moving in any directions also moves camera - this is how player can change mouse.Hit property without firing mouse.Move event.
Let’s assume that you want to use blocks building system. A player moves his mouse. Block spawn location changes. Later the player moves his character forward without moving his mouse. Block spawn location is still in the same location.
So I don’t want you to comment on the rest of the code. I only want you to tell me what I should implement for distances from the mouse to the player and a max distance. Thanks.
local player = game:GetService("Players").LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local maxDistance = 32
while wait() do
local mouseHit = mouse.Hit
local mouseHitPosition = Vector3.new(mouseHit.X, mouseHit.Y, mouseHit.Z)
local rootPartPosition = humanoidRootPart.Position
local distance = (mouseHitPosition - rootPartPosition).Magnitude
print(distance)
if distance <= maxDistance then
-- Do building stuff
-- else do nothing
end
end
It almost worked but after I got it to 32 studs, it’s now red forever. It’s also doing this zooming glitch. I’ve re-written the code so that could possibly be the issue? I’ll give you the video:robloxapp-20210531-0829230.wmv (3.6 MB)
I’ll also give the code:
local BuildItems = {
game.ReplicatedStorage.BuildItems.SelectFrontRoof:Clone(),
game.ReplicatedStorage.BuildItems.SelectWalls:Clone()
}
local player = game:GetService(“Players”).LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local humanoidRootPart = character:WaitForChild(“HumanoidRootPart”)
local mouse = player:GetMouse()
local maxDistance = 32
script.Parent.MouseButton1Click:Connect(function()
mouse.Move:Connect(function()
for i = 1, 1 do
while wait() do
local mouseHit = mouse.Hit
local mouseHitPosition = Vector3.new(mouseHit.X, mouseHit.Y, mouseHit.Z)
local rootPartPosition = humanoidRootPart.Position
local distance = (mouseHitPosition - rootPartPosition).Magnitude
print(distance)
if distance <= maxDistance then
BuildItems[i].Position = mouse.Hit.Position
BuildItems[i].Parent = workspace
else
BuildItems[i].BrickColor = BrickColor.new(“Really red”)
BuildItems[i].Position = mouse.Hit.Position
BuildItems[i].Parent = workspace
end
end
end
end)
end)
I would fire the server with the mouse position, then calculate the distance from the player and the mouse on the server, and then check if the distance is smaller than the range you have given because if you do all of that on the client it’s too easy to exploit.
Okay, so you send the mouse position over to the server as you said via a remote event and then check the distance between the player and his mouse position like this :
LocalScript:
local Mouse = Player:GetMouse()
then you fire the remote event like if the player clicks or something idk.
remote:FireServer(Mouse.Hit)
ServerScript:
local Range = 150
remote.OnServerEvent:Connect(function(Player, Mouse)
local Distance = (Mouse.Position - HumanoidRP.Position).Magnitude
if Distance < Range then
– do stuff
end
end)
Thanks! It worked but now I need a fix for this bug. It keeps doing this weird glitch where it zooms out and I also need it to turn back green once the distance is not equal to or more than the max distance. I’ll show you what happens when I play: robloxapp-20210531-0829095.wmv (497.6 KB)