for part 3, i want the script to not be within the part but accessed elsewhere. I also want the parts to only be collected per player so that if a player collects a part, it does not disappear for other players.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Got it! We’ll refactor the script to handle collectibles outside the part and ensure each player sees the collectible disappear only for themselves (client-side). Here's how to do it:
🛠️ Updated Solution: Collectibles Per Player (Client-Side Visibility)
Steps to Implement:
Move the Script to a central location (e.g., ServerScriptService).
Use RemoteEvents to communicate between the server and each player's client.
Clone collectibles for each player so they disappear individually.
🧩 Part 1: Set Up RemoteEvent
Create a RemoteEvent to tell the client when a collectible is picked up.
1. Insert RemoteEvent in ReplicatedStorage
Go to ReplicatedStorage
Insert > RemoteEvent
Rename it to CollectiblePicked
🧩 Part 2: Server-Side Script (Handles Logic)
1. Insert Script in ServerScriptService
lua
-- ServerScriptService/CollectibleHandler
local collectibleTemplate = workspace.Collectible -- Original collectible part
local replicatedStorage = game:GetService("ReplicatedStorage")
local collectiblePickedEvent = replicatedStorage:WaitForChild("CollectiblePicked")
-- Clone collectibles for each player
local function setupPlayer(player)
local playerCollectibles = {}
-- Clone all collectibles for this player
for _, original in pairs(workspace:GetChildren()) do
if original.Name == collectibleTemplate.Name then
local clone = original:Clone()
clone.Parent = workspace
clone.Name = original.Name .. "_" .. player.Name -- Unique name
table.insert(playerCollectibles, clone)
end
end
-- Connect Touched event for player's collectibles
for _, clone in pairs(playerCollectibles) do
clone.Touched:Connect(function(hit)
local character = hit.Parent
local playerWhoTouched = game.Players:GetPlayerFromCharacter(character)
if playerWhoTouched == player then
-- Fire event to the specific player's client
collectiblePickedEvent:FireClient(playerWhoTouched, clone)
-- Update server-side score
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local score = leaderstats:FindFirstChild("Score")
if score then
score.Value += 1
end
end
end
end)
end
end
-- Handle new players joining
game.Players.PlayerAdded:Connect(function(player)
setupPlayer(player)
-- Leaderstats setup (same as before)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local score = Instance.new("IntValue")
score.Name = "Score"
score.Value = 0
score.Parent = leaderstats
end)
🧩 Part 3: Client-Side Script (Handles Visibility)
1. Insert LocalScript in StarterPlayerScripts
lua
-- StarterPlayerScripts/CollectibleVisibilityHandler (LocalScript)
local player = game.Players.LocalPlayer
local replicatedStorage = game:GetService("ReplicatedStorage")
local collectiblePickedEvent = replicatedStorage:WaitForChild("CollectiblePicked")
-- Listen for collectible picked event
collectiblePickedEvent.OnClientEvent:Connect(function(collectible)
-- Destroy the collectible only for this player
collectible:Destroy()
end)
🧩 Part 4: Prepare Collectible Parts
Original Collectible:
Place your Collectible part in Workspace.
Ensure it’s Anchored.
Duplicate for Testing:
Duplicate the Collectible part a few times to test multiple collectibles.
🌟 How It Works:
Server:
Clones collectibles for each player when they join.
Tracks which player touches their own cloned collectible.
Fires the CollectiblePicked event to the correct player.
Client:
Listens for the event and destroys the collectible only for that player.
🧪 Test It:
Press Play and join with 2+ players.
Each player should see all collectibles.
When a player collects one, it disappears only for them, not others.
🚀 Next Steps (Optional):
Respawn Collectibles:
Add a delay and re-clone the collectible after it’s picked up.
Optimize Cloning:
Use a Folder to organize player-specific collectibles.
Add Effects:
Play a sound or particle effect when collected.
Let me know if you’d like to add any of these features, or if you need help troubleshooting!