How to create local parts with scripts in it?

  1. What do you want to achieve?
    So basically Im trying to make something like “collecting” parts and whenever player hit the part, part dissapear but there is problem, I want to achieve that part dissapear only for one person.

  2. What solutions have you tried so far?
    I search youtube, developer forum but there’s no solution how to do it for more parts with scripts in it and I’ll really appreciate your help

This is how my code looks like now:

model = script.Parent
local required = 5

backup = model:clone()
enabled = true

function regenerate()
	model:remove()
	wait(math.random(1,5))
	model = backup
	model.Parent = game.Workspace
	model:makeJoints()
end

local debounce = false

function onHit(hit)

	debounce = false

	if not debounce then

		debounce = true

		if (hit.Parent:FindFirstChild("Humanoid") ~= nil) and  enabled then
			wait(0.1)
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr.leaderstats.Example.Value < plr.leaderstats.Storage.Value then
				plr.leaderstats.Example.Value = plr.leaderstats.Example.Value + 1 
				regenerate()
				wait(0.5)
				debounce = false
			elseif plr.leaderstats.Example.Value == plr.leaderstats.Storage.Value then
				print("full")
				local new = plr.PlayerGui:WaitForChild("PopUpCurrency"):WaitForChild("FullStorage")
				wait(0.2)
				new.Visible = true
				wait(0.7)
				new.Visible = false
				wait(0.2)
				debounce = false
			end
		end
	end
end
script.Parent.Touched:Connect(onHit)
2 Likes

Then copy the script,delete the script and make a local script with the same script in it and yeah

1 Like

Anything done on a local script is only seen by the player the local script belongs to.

I don’t really know where the script is but you will have to change the last line

You cannot do this just for the sake that the part is local, and LocalScripts won’t run in it. However, you don’t need the script to be inside of it for it to work. Change all of the script.Parents to the location of the part, and you can use it as a LocalScript instead. If there are more than one such parts, you can communicate between LocalScripts with BindableEvents. Or you can create an ObjectValue in the script so that it knows the location of the part, and copy the script with a new ObjectValue for each new part. Up to you.

Sorry I forgot to mention it, all parts is in workspace with scripts included

Yeah no i don’t know how i will explain that

Yes but I’m not sure how can I make 20 local scripts for parts with same name localized in StarterPlayerScripts.

Thank you! I’m now one more step ahead!

Scripts in the workspace are not local scripts. Local scripts will only run if the script is a descendant of certain objects as seen here (docs). Scripts run on the server, while localscripts run on each individual player’s client.

Making a part disappear for only one player can be done by making the part disappear within a local script. Since the part only disappears on the individual client, the rest of the players will not see the part disappearing.

To accomplish this, I would try creating a localscript that detects when the player hits something. Then, check to see if the part is a “collecting” part and if it is, make the part disappear.

Loops or events or both. One LocalScript to control them all.

Either do it from the perspective of the part as you have been, or do it from the character - every time your character touches something, check if it’s collectable and if so do the logic on that part they touched.

Okay so I am thinking about to do what you said so If player touch “collectable” part then it fire event, but where should I place the actual local script that will run “Destroy” function in playergui or starterplayerscripts and also ty for your answear!

And also I have big problem with objectvalue since when “collectable” item got destroyed and regenerate part isn’t in objectvalue then. :confused:

If it’s linked to a particular GUI, then you can put it in a Gui.

Otherwise I’d just put it in StarterPlayerScripts. Be sure to consider the case of respawning and always to get the new character by hooking to CharacterAdded.

To regenerate the part, you can save a backup as part of the code when you destroy the part.

local regenerateTime = 30 -- seconds

function destroyAndRegenerate( part )
    local backup = part:Clone()
    local parent = part.Parent
    part:Destroy()
    delay( regenerateTime, function()
        backup.Parent = parent
    end )
end

bottom-up approach usually provokes circular dependencies; you cannot do that