Recently I have been working on a custom pick up method. I used a free model as a template then edited the scripts to suit my needs (pls don't hate xddd). You would go up to the weapon, press Q and the weapon would disappear and you would find it in your inventory. When I go on Studio Test all is fine but once I test a server then I can only see the gui to pick up the item and when I press Q nothing happens. I also made it so the GUI disappears after 3 seconds. After the 3 seconds are gone, the gui disappears and never comes back once i touch the hitbox again.... Does it have to do with Filtering Enabled? How can I fix this issue?
01 | Player = game.Players.LocalPlayer |
02 | Mouse = Player:GetMouse() |
03 | tool = script.Parent.ToolData |
04 | k = script.Parent.HotkeyData |
05 | buttonPressed = false |
06 |
07 | function Press(key) |
08 | if (key = = k.Value) and game.ServerStorage:FindFirstChild(tool.Value) ~ = nil then |
09 |
10 | if Player.Backpack:FindFirstChild(tool.Value) = = nil and Player.Character:FindFirstChild(tool.Value) = = nil then |
11 | local weapon = game.ServerStorage:FindFirstChild(tool.Value):clone() |
12 | weapon.Parent = Player.Backpack |
13 | repeat |
14 | Player.PlayerGui:FindFirstChild( "HotKeyGuiThing" ):remove() |
15 | until Player.PlayerGui:FindFirstChild( "HotKeyGuiThing" ) = = nil |
This is a problem with FilteringEnabled yes. A script cannot listen to the mouse.KeyDown event, because it runs on the server. A localscript cannot clone items from serverstorage and give them to a player:
You need to separate these two things by listening to the mousclick in a localscript, firing a remoteEvent on the server, and listening to that event by a script.
Keep in mind that scripts should not be put in the startergui and localscripts should not be put in workspace.
Also I recommend using Destroy() over Remove() (remove is also deprecated)
Finally to interact with things in workspace, it's better to check if this item exists before applying functions to it:
1 | if workspace:FindFirstChild( "Weapon Crate" ) then workspace [ "Weapon Crate" ] :Destroy() end |
Good luck!