Hi,
I am developing a script that when a player forages a piece of fruit, the glue that holds the fruit should be deleted. My solution to this is to apply a remote event that activates when the fruit is foraged, that should destroy all the glue objects in the model (because if the glue objects are not destroyed, the player after putting the fruit back into the workspace will cause the fruit to "travel" back to the glue object). The following is my code for the remote event:
local ReplicatedStorage= game:GetService("ReplicatedStorage")
local createItemForageEvent= Instance.new("RemoteEvent",ReplicatedStorage)
createItemForageEvent.Name= "CreateItemForageEvent"
local function onMessageDeliveredToServer2(sender,mouseTarget,str)
--if item forages has no isInventory then add one
if mouseTarget.Name== "Sand" then
mouseTarget= game.ReplicatedStorage:FindFirstChild("Pile of Sand",true):Clone()
elseif mouseTarget.Name== "Snow" then
mouseTarget= game.ReplicatedStorage:FindFirstChild("Pile of Snow",true):Clone()
elseif mouseTarget.Name== "Pool of Oil" then
mouseTarget= game.ReplicatedStorage:FindFirstChild("Crude Oil",true):Clone()
--[[
elseif (mouseTarget.Name=="Berry" or mouseTarget.Name=="Apple") and
mouseTarget.Parent~= workspace then
local descendants=mouseTarget.Parent:GetDescendants()
for index, descendant in pairs(descendants) do
--print(descendant.Name)
if descendant.Name=="Glue" then
descendant:Destroy()
end
end
--]]
end
if mouseTarget:FindFirstChild("IsInventory")== nil then
local addIsInventory= Instance.new("IntValue",mouseTarget)
addIsInventory.Name= "IsInventory"
end
--mouseTarget.Parent= game.StarterGui.Inventory.Itembag
mouseTarget.Locked= false
mouseTarget.Anchored= true
mouseTarget.CanCollide= false
mouseTarget.Transparency= 0.5
mouseTarget.Parent= game.ReplicatedStorage.PlayerFiles:FindFirstChild(tostring(sender)).ItemBag
--print(game.StarterGui.Inventory.UDim2X.Value)
--print(game.StarterGui.Inventory.UDim2Y.Value)
end
createItemForageEvent.OnServerEvent:Connect(onMessageDeliveredToServer2)
The following is an example of the apple tree model directory I am working with: Apple Tree (Model)
-Apple
-Apple
-Apple
-Apple
-Apple
-Large Leaves
--Glue
--Glue
--Glue
--Glue
--Glue
--Snap
--Flammable (value)
-Small Leaves
-Small Tree Stump
Bascally, the code (in commented) gets the apple's parent, searches all descendants for "Glue" then removes them, all in the remote event. When I published and tested online, this special mechanism does not work, and the apple travels back to the glue location, meaning the glue did not get removed.
My secondary solution would be to add a script in the model that detects if a child was removed, then delete the glue, but this is not a desirable solution (more tedious and against KISS principle).
Workspace.ChildRemoved:Connect(function(instance)
if instance=="Apple" then
local descendants=mouseTarget.Parent:GetDescendants()
for index, descendant in pairs(descendants) do
--print(descendant.Name)
if descendant.Name=="Glue" then
descendant:Destroy()
end
end
end
end)
I am not sure again what is going on since putting this in a remote event should work the same as if I am online.
Thanks, Houlardy
