How can I change the position of a part using a local script inside of a tool

I’m trying to make a magic wand that levitates rocks into the air when you click on them. The script I have in place currently works, but it lifts ALL the rocks at the same time instead of just the target rock that is clicked.

Here is my local script inside the tool

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Up:Connect(function()
	local target = mouse.Target
	--print(target)
	if target.Name == "Rock" then
		game.Workspace.RemoteEvents.LiftRock:FireServer()
	else return 
	end
	
end)

Then here is the script I have pasted into each rock:

local TweenService = game:GetService("TweenService")
local part = script.Parent
part.Position = part.Position
part.Anchored = true
part.Parent = game.Workspace

local goal = {}
goal.Position = part.Position + Vector3.new(0, 10, 0)

local tweenInfo = TweenInfo.new(1)

local tween = TweenService:Create(part, tweenInfo, goal)

local deb = false

local function raiseRock()
	if deb == false then
		deb = true
		tween:Play()

		for i = 1,10,1 do
			part.Highlight.OutlineTransparency = part.Highlight.OutlineTransparency - .1
			part.Highlight.FillTransparency = part.Highlight.FillTransparency - .08
			wait(.1)
		end

	end

end

game.Workspace.RemoteEvents.LiftRock.OnServerEvent:Connect(raiseRock)

I thought it may have been that I was using a folder for remote events instead of replicated storage, though moving the remote event and changing the script did not allow me to click multiple rocks. It ran once and would not let me click the others.

I have tried passing in the “target” variable from the LocalScript to the server script, and everywhere I have tried seems to suggest the same thing, but it’s registering as the player instead of the clicked part.

How do I pass the target of the mouse into the server script?

You could send data to the server on which Rock to lift and changing the variables a bit (not exactly sure what part is)

Local Script:

if target.Name == "Rock" then
		game.Workspace.RemoteEvents.LiftRock:FireServer(target)

Server Script:

local function raiseRock(target)
	if deb == false then
		deb = true
		local goal = {}
		goal.Position = target.Position + Vector3.new(0, 10, 0)
		local tween = TweenService:Create(target, tweenInfo, goal)
		tween:Play()

Yeah that is what I tried before. When I put “target” in the parentheses for both the scripts it keeps saying “position is not a valid member of Player.” The remote event is triggered by the player, so I have a feeling that’s why it is passing in.

The first parameter of a .OnServerEvent is always the player, just change it the function variables to this.

local function raiseRock(player, target)

Then I get “attempt to index nil with position.” This happens when I do (player, target) in both scripts as well as just in the server script. I have a print command that is able to detect that I’m clicking a rock in the local script, but not in the server script.

No no, do not do player as a variable in the FireServer, the player gets send automatically, only send the target.

		game.Workspace.RemoteEvents.LiftRock:FireServer(target)

Alright, I think it’s working now. I just need to figure out how to only make the rocks lift once. The debounce in my script before was only allowing me to lift one and stops them all from lifting after that.

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