Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Using RemoteEvent to remove Mouse.Target from server?

In making some inventory system that i can pick up items on the ground ,when im close enough i can click "E" to pick up the item, the item is then added in my inventory and should be removed from the workspace, The problem here is to remove the item from workspace i'd use Mouse.Target:Destroy() but now i can't use it from the serverScript. How could i do this?

Heres the LocalScript to pick up item

local player = game:GetService("Players").LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Mouse = player:GetMouse()


Mouse.KeyDown:connect(function(key)
    key = key:lower() 
    if ( key == "e") then 
        local pos1 = Mouse.Target.Position
        local pos2 = player.Character.Torso.Position
        local magnitude = (pos1 - pos2).magnitude
	local removeItemEvent = ReplicatedStorage:WaitForChild("RemoveItemEvent")
        if magnitude <=6 and Mouse.Target.Parent:FindFirstChild("Type") then
            local name = Mouse.Target.Parent
			local id = Mouse.Target.Parent.ID
			local inv = player.PlayerGui.UI.Right.Inventory.Inv
			local Type = Mouse.Target.Parent.Type
	
			if inv.Items.Value < 12 then
				inv.Items.Value = inv.Items.Value + 1
				local button = inv.ImageButton:Clone()
				button.Name = name.Name
				button.Parent = inv
				button.ItemType.Value = Type.Value
				button.Visible = true
				--button.Image = id.Value
				removeItemEvent:FireServer()
				print(Mouse)	
			end
        end
    end
end)

here's the serverScript that sould remove the item from the workspace so everyone on the server see it getting removed.


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
 
local removeItemEvent = ReplicatedStorage.RemoveItemEvent
 
local function onRemoveItemFired(player)
	player = game:GetService("Players"):FindFirstChild(player.Name)
	local Mouse = player:GetMouse()
	print(player,Mouse.Target)
	Mouse.Target.Parent:Destroy()
end
 
removeItemEvent.OnServerEvent:Connect(onRemoveItemFired)

1 answer

✓ Accepted answer

You cannot use GetMouse() on the server. You need to fire the RemoteEvent with Mouse.Target instead, and then do stuff with that part.

BUT!!!! Expl0iters can fire the remote with anything, so you need to perform some checks in order to prevent them from picking up items from the other side of the map. One way to do this would be to check if they are close to the item, using magnitude.

2 comments

0

ohh i think i got it! thanks!

0

Yw. Feel free to accept the answer if you've found it helpful!

This discussion is preserved as it was. New answers, comments, and votes are closed.