Here’s the hierarchy of the Wooden plank
Here’s the hierarchy of the Folder where wooden planks spawn
Here’s the script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local remoteEvent = ReplicatedStorage.WoodCollectionEvent
local woodenPlankModel = ReplicatedStorage.WoodModel
local spawnPlacesFolder = workspace.QuestItemsSpawnPlaces
local function playAnimation(plank)
local animationController = Instance.new("AnimationController")
animationController.Parent = plank
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=16754315615" -- Your animation ID
local animationTrack = animationController:LoadAnimation(animation)
if animationTrack then
animationTrack:Play()
else
warn("Failed to load animation in AnimationController")
end
end
local function spawnWoodenPlanks()
-- Get all potential spawn places
local spawnPlaces = spawnPlacesFolder:GetChildren()
-- Shuffle the spawn places
for i = #spawnPlaces, 2, -1 do
local j = math.random(i)
spawnPlaces[i], spawnPlaces[j] = spawnPlaces[j], spawnPlaces[i]
end
-- Counter for wooden planks spawned
local plankCount = 0
-- Spawn wooden planks on up to 3 random places
for _, place in ipairs(spawnPlaces) do
if plankCount >= 3 then
break
end
-- Check if there's no wooden plank already spawned in this place
local existingPlank = place:FindFirstChild("WoodModel")
if not existingPlank then
local newPlank = woodenPlankModel:Clone()
newPlank:SetPrimaryPartCFrame(place.CFrame) -- Set the position of the plank to match the spawn place
newPlank.Parent = place
playAnimation(newPlank) -- Play animation when a new plank is spawned
plankCount = plankCount + 1
end
end
end
-- Function to handle collision with wooden planks
local function onCollision(hit)
-- Check if the object colliding is a player
local character = hit.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
-- Trigger the event
remoteEvent:FireServer() -- Assuming the server handles this event
-- Fade out the wooden plank
local tweenInfo = TweenInfo.new(
1, -- Duration
Enum.EasingStyle.Linear, -- Easing style
Enum.EasingDirection.Out, -- Easing direction
0, -- Repeat count (zero means no repeat)
false, -- Reverses on repeat
0 -- Delay
)
local fadeOutTween = TweenService:Create(hit.Parent, tweenInfo, {Transparency = 1})
fadeOutTween:Play()
-- Destroy the plank after fade out
fadeOutTween.Completed:Connect(function()
hit.Parent:Destroy()
end)
end
end
-- Trigger spawnWoodenPlanks function when remote event is fired
remoteEvent.OnClientEvent:Connect(function(player)
spawnWoodenPlanks()
end)
-- Connect the collision event for wooden planks
for _, place in ipairs(spawnPlacesFolder:GetChildren()) do
local plank = place:FindFirstChild("WoodModel")
if plank then
plank.EggMesh.Touched:Connect(onCollision)
end
end
The animation works, but I can’t pick it up. I wanted to clone wooden planks for every player locally and I’m just missing the pick up for it to work, I’ve tried different methods but I don’t know how to make it with a local script.
This local script is part of player GUI and not the model itself