Hello Devs! This might be very simple for some devs but I could not manage to do It by myself and I need help, I have a star system when you touch It, It collects and deletes on the local side, so other players can get that star too, Instead of making multiple scripts In stars, I decided to make one script that gets all the stars and do the function, I am not sure If this will make any changes In performance If anyone knows If this changes anything please let me know, also I cannot make the script to work, It works If I make the script separately into the stars but whenever I try to get childer and destroy the stars It destroys the character Instead of the stars. Any help Is greatly appreciated!
Server-Script
local star = script.Parent:GetChildren()
-- Function to handle the touch event
local function onTouch(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
-- Using RemoteEvent to communicate with the client
local remoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
if remoteEvent then
-- Fire the event only to the player who touched the star
remoteEvent:FireClient(player, star)
end
end
end
-- Connect the onTouch function to the Touched event of the star
star.Touched:Connect(onTouch)
StarterPlayerScripts: local star = script.Parent:GetChildren()
-- Function to handle the touch event
local function onTouch(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
-- Using RemoteEvent to communicate with the client
local remoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
if remoteEvent then
-- Fire the event only to the player who touched the star
remoteEvent:FireClient(player, star)
end
end
end
-- Connect the onTouch function to the Touched event of the star
star.Touched:Connect(onTouch)
StarterPlayerScripts:
local remoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
remoteEvent.OnClientEvent:Connect(function(starFullName)
-- Use the full name to locate the star part
local starPart = game.Workspace:FindFirstChild(starFullName, true)
if starPart and starPart.Name == "Star" then
print("Destroyed star:", starPart)
starPart:Destroy() -- Destroy only the received part
else
print("Received non-star object or error with star reference:", starFullName)
end
end)