Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make a part spawn through localscript visible for others ?

I am making a localscript that spawns a part in a localplayer, But I am new to scripting and when I tested it out, other players didn't see the part, how can I fix it ? When I tried normal script it didn't work. The localscript is in Player's character, here is the script:

a = Instance.new("Part", workspace)
a.Size = Vector3.new(50,10,50)
a.Anchored = true
a.Name = "corv"
a.CanCollide = false
a.Transparency = 0
a.Position = game.Players.LocalPlayer.Character.LowerTorso.Position

2 answers

✓ Accepted answer

When you write code in a LocalScript it only shows on your computer, not everyone else's.

If you want to show the part for everyone, you're going to have to create the part on the server. You can do this using remote events.

Here's how you'd to that:

  1. Create a LocalScript in the StarterPlayerScripts

  2. Create a remote event in the ReplicatedStorage, call it createPartEvent

  3. Put the following code in the localscript:

 --reference the remote you made:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent= ReplicatedStorage:WaitForChild("createPartEvent")
 
-- Fire the remote event (tell the server you want to make a part)
createPartEvent:FireServer() 
  1. Create a normal script in the ServerScriptService

  2. Put the following code in the script:

--reference the remote event: 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent= ReplicatedStorage:WaitForChild("createPartEvent")
 
-- function that creates a part 
--this function gets ran whenever the remote event is fired because
--we have the code: "createPartEvent.OnServerEvent:Connect(createPart)"
--which runs the function "createPart" whenever you say "createPartEvent:FireServer()" in a localscript.

function createPart(player)
	--your code for creating the part is here:
	print(player.Name .. " fired the remote event")
	a = Instance.new("Part")
	a.Parent = workspace
	a.Size = Vector3.new(50,10,50)
	a.Anchored = true
	a.Name = "corv"
	a.CanCollide = false
	a.Transparency = 0
	a.Position = player.Character.LowerTorso.Position 
end
 
-- Call "createPart()" when a player fires the createPart remote event
createPartEvent.OnServerEvent:Connect(createPart)

Let me know if you have any questions.

This youtube video explains remote events/remote functions fairly well

this is a wiki article on it if you prefer to read. They actually go over how to do what you're trying to do in the Client to Server section of the article.

3 comments

0

Thanks

0

also should not 13th line in 4) be "local a = Instance.new("Part") ?

0

yes just wasnt paying attention when copying your code 

you can't

if you want to make a part visible to others, you have to create it on the server

3 comments

0

How am I supposed to do that ?

0

ever heard of remote events

0

ever heard of not being an a hole?

This discussion is preserved as it was. New answers, comments, and votes are closed.