Problem with remote event things

LocalScript

local Button = script.Parent

Button.Activated:Connect(function(Hit)
	local Mouse = game.Players.LocalPlayer:GetMouse()
	local MousePos = Mouse.Hit.Position

	Mouse.Button1Up:Connect(function()
	local Click = Mouse.Button1Up
	local MousePos2 = Mouse.Hit.Position
	Button.RemoteEvent:FireServer(MousePos, Click, MousePos2)
	end)
end)

Script

local Button = script.Parent

Button.RemoteEvent.OnServerEvent:Connect(function(Player, MousePos, Click, MousePos2)
	print("Fired")
	local NewPart = Instance.new("Part")
	NewPart.Size = Vector3.new(2, 2, 2)
	NewPart.Anchored = true
	NewPart.Transparency = 0.5
	NewPart.Position = MousePos
	NewPart.Parent = workspace

	if NewPart.Parent == workspace then
		repeat
			print("Repeating")
			NewPart.Position = MousePos2
			wait()
		until
		Click
	end
end)

Problem :roll_eyes: :cold_sweat:


As you can see, the “NewPart” doesn’t appear. How can I solve this problem?
(I want it to appear after the initial click of “ImageButton”)

1 Like

It doesn’t update properly because you are only sending the remote once, which means it’s just repeatedly positioning itself to the same spot.
Personally though, I’d make the preview appear on the client instead. Then once you’ve clicked, fire a remote to the server and place the part there.

I made the preview client-sided. Though I’m still facing the same problem of making the preview actually visible.

LocalScript

local Button = script.Parent

Button.Activated:Connect(function(Hit)
	local Mouse = game.Players.LocalPlayer:GetMouse()
	local MousePos = Mouse.Hit.Position

	Mouse.Button1Up:Connect(function()
		local Click = Mouse.Button1Up
		local MousePos2 = Mouse.Hit.Position
		
		local NewPart = Instance.new("Part")
		NewPart.Size = Vector3.new(2, 2, 2)
		NewPart.Anchored = true
		NewPart.Transparency = 0.5
		NewPart.Position = MousePos
		NewPart.Parent = workspace
		
		if NewPart.Parent == workspace then
			repeat
				print("Repeating")
				NewPart.Position = MousePos2
				wait()
			until
			Click
		end
		NewPart:Destroy()
		
		local Pos = NewPart.Position
		Button.RemoteEvent:FireServer(MousePos, Click, MousePos2, Pos)
	end)
end)
1 Like

I believe you are still setting the NewPart position to your old mouse position. It doesn’t update with your new one because MousePos is a reference to your old mouse position.

You wanna make it something like this

if NewPart.Parent == workspace then
	repeat
		print("Repeating")
		MousePos2 = Mouse.Hit.Position -- this updates the variable to your new mouse position
		NewPart.Position = MousePos2
		task.wait()
	until Click
end

Well, this might not solve your problem, but I wanna tell you that registering Mouse.Button1Up every time when ImageButton is activated gonna cause a memory leak if done a lot of times.

Instead of doing :Connect() you may wanna do :Once()

1 Like

Thanks, that stopped it from printing multiple times

if NewPart.Parent == workspace then
			repeat
				print("Repeating")
				local MousePos2 = Mouse.Hit.Position
				NewPart.Position = MousePos2
				NewPart.Parent = workspace
				wait()
			until Click
		end

It’s not appearing, the “NewPart” isn’t even in the workspace

Probably because it’s getting destroyed immediately after it gets created. You may wanna rewrite the repeat until section of your code and only let it repeat until you’ve actually clicked, instead of checking the event itself.

local Button = script.Parent

Button.Activated:Connect(function(Hit)
	local Mouse = game.Players.LocalPlayer:GetMouse()
	local MousePos = Mouse.Hit.Position

	Mouse.Button1Up:Once(function()
		local Click = Mouse.Button1Up
		local MousePos2
		
		local NewPart = Instance.new("Part")
		NewPart.Size = Vector3.new(2, 2, 2)
		NewPart.Anchored = true
		NewPart.Transparency = 0.5
		NewPart.Position = MousePos
		NewPart.Parent = workspace
		
		if NewPart.Parent == workspace then
			repeat
				print("Repeating")
				MousePos2 = Mouse.Hit.Position
				NewPart.Position = MousePos2
				task.wait()
			until Click
		end
		
		local Pos = NewPart.Position
		Button.RemoteEvent:FireServer(MousePos, Click, Pos)
	end)
end)

It’s not appearing before the click though

local Button = script.Parent

Button.Activated:Connect(function(Hit)
	local Mouse = game.Players.LocalPlayer:GetMouse()
	local MousePos = Mouse.Hit.Position
	
	local NewPart = Instance.new("Part")
	NewPart.Size = Vector3.new(2, 2, 2)
	NewPart.Anchored = true
	NewPart.Transparency = 0.5
	NewPart.Position = MousePos
	NewPart.Parent = workspace
	
	while NewPart.Parent == workspace do wait()
		MousePos = Mouse.Hit.Position
		NewPart.Position = Vector3.new(MousePos.X, 1, MousePos.Z)

		local Click = Mouse.Button1Up
		Click:Connect(function()
			print("Click")
			if NewPart.Parent == workspace then
				NewPart:Destroy()
			end
		end)
	end
	local Pos = NewPart.Position
	Button.RemoteEvent:FireServer(MousePos, Pos)
end)

Did something. I want to stop the while loop though, how do I do that?
I tried using “break”, didn’t work very well

LocalScript

local Button = script.Parent

Button.Activated:Connect(function(Hit)
	local Mouse = game.Players.LocalPlayer:GetMouse()
	local MousePos = Mouse.Hit.Position
	
	local NewPart = Instance.new("Part")
	NewPart.Size = Vector3.new(2, 2, 2)
	NewPart.Anchored = true
	NewPart.Transparency = 0.5
	NewPart.Position = MousePos
	NewPart.Parent = workspace
	local Pos
	
	while NewPart.Parent == workspace do wait()
		MousePos = Mouse.Hit.Position
		NewPart.Position = Vector3.new(MousePos.X, 1, MousePos.Z)

		local Click = Mouse.Button1Up
		Click:Once(function()
			print("Click")
			if NewPart.Parent == workspace then
				Pos = NewPart.Position
				print(Pos)
				NewPart:Destroy()
			end
		end)
	end
	Button.RemoteEvent:FireServer(MousePos, Pos)
end)

Script

local Button = script.Parent

Button.RemoteEvent.OnServerEvent:Connect(function(Player, MousePos, Pos)
	print("Fired")
	local NewPart = Instance.new("Part")
	NewPart.Size = Vector3.new(2, 2, 2)
	NewPart.Anchored = true
	NewPart.Transparency = 0
	NewPart.Position = Pos
	print(NewPart.Position)
	NewPart.Parent = workspace
end)

A lot of this can be simplified. I recommend you put the ServerScript in a safe place like ServerScriptService, as well as the RemoteEvent in ReplicatedStorage. As for the LocalScript, it is up to you to keep it inside the button or have it in StarterPlayerScripts (Where should I be storing my Local Scripts?)

LocalScript

-- In the Button or StarterPlayerScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local ButtonEvent = ReplicatedStorage.ButtonEvent

local Mouse = Players.LocalPlayer:GetMouse()

local Button = -- your button location

Button.MouseButton1Up:Connect(function()
	local MousePos = Mouse.Hit.Position
	ButtonEvent:FireServer(MousePos)
end)

Script

-- In ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ButtonEvent = ReplicatedStorage.ButtonEvent

ButtonEvent.OnServerEvent:Connect(function(Player, MousePos)
	print("Fired")
	local NewPart = Instance.new("Part")
	NewPart.Size = Vector3.new(2, 2, 2)
	NewPart.Anchored = true
	NewPart.Transparency = 0.5
	NewPart.Position = MousePos
	NewPart.Parent = workspace

end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.