What I want achieved is the possibility to be able to place a model when you press a Screen GUI Button, everybody can see the model. The current model name is NPC, when the user places the Human, it names it, for example, 'RobloxGameingStudios_NPC'
NPC is where the model name is, and RobloxGameingStudios is the user who placed it's name. like: 'ROBLOX_NPC',
This tells us ROBLOX placed a NPC.
The reason I want it to be named that is because It would be easier to edit the model that way, Ill tell u more about that when the game comes out.
I'm good enough, that I realize this is going to need a remote event or two, and a server-sided script.
Script Attempt:
Local Script Inside the button:
script.Parent.MouseButton1Click:Connect(function() game.ReplicatedStorage.PlaceHuman:FireServer() end)
Server-Sided Script:
game.ReplicatedStorage.PlaceHuman.OnServerEvent:Connect(function() BlockName = "NPC" local block = game.ReplicatedStorage.Blocks[BlockName]:Clone() local x = math.floor(pos.X) local y = math.floor(pos.Y) local z = math.floor(pos.Z) local BlockSize = block.Size.X block.Position = Vector3.new(x + BlockSize/2, y + BlockSize/2, z + BlockSize/2) if passive then block.Transparency = 0 block.Anchored = true block.CanCollide = true block.Name = game.Players.LocalPlayer.. "_" ..BlockName end block.Parent = workspace.Blocks[plr.Name] end)
What game am I making? i'm trying to make a god-like game.
If what you're trying to achieve is using the PlayerObject
's name, then I can help you here. A helpful tip is to remember that the local PlayerObject
is actually sent as an argument through FireServer
and can be picked up as a parameter. It is advised you use this method as the Server cannot actually access the local PlayerObject
—because it's the Server, not a Client
—which is why line 14
will not work. This is what you're looking for:
-- LocalScript local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent script.Parent.MouseButton1Click:Connect(function() RemoteEvent:FireServer() end) -- ServerScript local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent RemoteEvent.OnServerEvent:Connect(function(Player) print(Player.Name.."'s_NPC") end)
Using SetPrimaryPartCFrame to your advantage
With greatneil80's suggestion, applying it is probably the best course of action for making this program easier to function and easier to use. Using block
as the NPC model you desire to place within the workspace, we can apply this metamethod. Firstly, let's understand SetPrimaryPartCFrame
.
The Primary Part of a model is the piece that everything wraps onto, think of it as the foundation. For that, when we move the foundation, everything else should follow. The metamethod of Model
, SetPrimaryPartCFrame
, will adjust that foundations coordinates to whatever specified, as long as it's a Vector3
input for the new CFrame
. Luckily, we can actually get the Vector3
positional values of the mouse
, which means we can ask SetPrimaryPartCFrame
to place the NPC wherever our mouse is located; for that to work, we need to get access to the cursor. All of this will finally be represented in this Script:
-- LocalScript local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent local Cursor = game:GetService("Players").LocalPlayer:GetMouse() Cursor.Button1Down:Connect(function() RemoteEvent:FireServer(Cursor.Hit.p) -- Hit is the CFrame, p is the Vector3 translation end) -- ServerScript local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent RemoteEvent.OnServerEvent:Connect(function(Player, CursorPos) -- Passed Vector3 local block = -- Pathway to the model :Clone() block.Parent = workspace block.Name = Player.Name.."'s_NPC" block:SetPrimaryPartCFrame(CFrame.new(CursorPos)) end)
Just a few more issues:
Just one more question, this works great and ill be accepting this answer, but because all my things wont be human's, is there any way I can + 1 to the CFrame so it always places on the ground?
also i tried adding a mouse button 1 click so when u click it, it turns on, but i need a way to turn it off, so u cant just place it forever