Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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() 

3) Create a normal script in the ServerScriptService

4) 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.

0
Thanks kikocraftak233 18 — 4y
0
also should not 13th line in 4) be "local a = Instance.new("Part") ? kikocraftak233 18 — 4y
0
yes just wasnt paying attention when copying your code  royaltoe 5144 — 4y
Ad
Log in to vote
-1
Answered by
Elyzzia 1294 Moderation Voter
4 years ago
Edited 4 years ago

you can't

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

0
How am I supposed to do that ? kikocraftak233 18 — 4y
0
ever heard of remote events EmK530 143 — 4y
0
ever heard of not being an a hole? Sadwowow21 57 — 4y

Answer this question