Basically, I’m making a game like Get a snack at 4 am, (Gasa4) and I’m trying to make it so you can only pick up one item at a time.
Here is my script:
game.Players.PlayerAdded:Connect(function(Player)
script.Parent.Attachment.ProximityPrompt.Triggered:Connect(function()
local item = script.Parent
local inv = Player:FindFirstChild("Inventory")
for _,v in pairs(inv:GetChildren()) do
if v:IsA("Part") then
print("Inventory Full!")
end
end
local inv = Player:FindFirstChild("Inventory")
if #inv:GetChildren() == 0 then
item.Parent = inv
end
end)
end)
The issue is that even if I have already picked up an item then threw it away, the item is still there on the server side, so it prints “Inventory Full”. Is this an issue with my trash can script? How do I make it so it removes it from the server side and the client side? Like I said this is a gasa4 styled game, so there is only 1 player max.
Here is my trash can script:
local prompt = workspace["Trash Can"].ProximityPrompt
script.Parent.ChildAdded:Connect(function(item)
prompt.Triggered:Connect(function(plr)
local inv = plr:FindFirstChild("Inventory")
item:Destroy()
inv:ClearAllChildren()
end)
end)
Is there anything I should be doing differently? How do I solve this?
I assume the first code you’ve given is in a Server Script, but what about the second? When you change something on the server, the client also gets affected by the change. If the second given code is in a LocalScript just switch to using a Server Script.
Side note: For the first script, you do not need the PlayerAdded event to get the player, as like what you did in the second script, the first parameter of the Triggered event is the player itself. Even if you intend to have your game at a 1 player server limit, it would still be considered bad practice.
local prompt = script.Parent:WaitForChild("Attachment"):WaitForChild("ProximityPrompt")
prompt.Triggered:Connect(function(Player)
local item = script.Parent
local inv = Player:FindFirstChild("Inventory")
for _,v in pairs(inv:GetChildren()) do
if v:IsA("Part") then
print("Inventory Full!")
end
end
local inv = Player:FindFirstChild("Inventory")
if #inv:GetChildren() == 0 then
item.Parent = inv
end
end)
Yes, the second script is a local script. But when I simply change it to a server script, it does not work. By the way, i should add that the second script is inside of a gui, the one for the inventory, if that changes anything. How can I rewrite it to be better suited for a server? I’m wondering if I should possibly use a Remote Event or something like that.
thats the gui and thats where the script is at. it doesnt work if I put it into the trash can model itself due to me being unable to reach the inventory in the script.
Yes, you should use RemoteEvents for this, and it shouldn’t be a complicated process. Insert a RemoteEvent in ReplicatedStorage, and rename it to InventoryEvent (or whatever else you want it to be).
Now fire the remote event in the LocalScript:
local RP = game:GetService("ReplicatedStorage")
local Event = RP:WaitForChild("InventoryEvent")
local prompt = workspace["Trash Can"].ProximityPrompt
script.Parent.ChildAdded:Connect(function(item)
prompt.Triggered:Connect(function(plr)
--local inv = plr:FindFirstChild("Inventory")
--inv:ClearAllChildren()
item:Destroy()
Event:FireServer() -- fire the remote event
end)
end)
Now in the same server script, use the following updated code:
local RP = game:GetService("ReplicatedStorage")
local Event = RP:WaitForChild("InventoryEvent")
local prompt = script.Parent:WaitForChild("Attachment"):WaitForChild("ProximityPrompt")
-- OnServerEvent
Event.OnServerEvent:Connect(function(player)
local inv = player:FindFirstChild("Inventory")
if inv then
inv:ClearAllChildren()
end
end)
-- Code from my previous post
prompt.Triggered:Connect(function(Player)
local item = script.Parent
local inv = Player:FindFirstChild("Inventory")
for _,v in pairs(inv:GetChildren()) do
if v:IsA("Part") then
print("Inventory Full!")
end
end
if #inv:GetChildren() == 0 then
item.Parent = inv
end
end)
Edit: I just saw your edit in your post above; switch back the TrashScript script to a LocalScript