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

How would I go about making a button that allows you to build models, such as a human?

Asked by 4 years ago

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.

0
have you simply tried using model:SetPrimaryPartCFrame(CFrame.new(m.Hit.p)) then getting the button1 and then firing the remotes..? greatneil80 2647 — 4y

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
0
Please also take note of greatneil80's comment, it will clean up your placement function, and make things smoother. If you need help applying that, please comment on my answer, if my answer was also what you were looking for, don't forget to accept it! Ziffixture 6913 — 4y
0
This greatly took care of the player side of things, But im still confused on how to get the building part to work! RobloxGameingStudios 145 — 4y
0
Could you tell me what isn't working for the placement? Ziffixture 6913 — 4y
0
With greatneil80's suggestion, I can help you place the NPC wherever you click? Ziffixture 6913 — 4y
View all comments (6 more)
0
if your asking, then yes i think i see what greatneil80 is trying to do, but I dont understand how it is supposed to connect to the player's mouse and place RobloxGameingStudios 145 — 4y
0
Let me configure my answer and explain Ziffixture 6913 — 4y
0
I know it's a pretty rough explanation, I can try to make it better if you still can't grasp it yet, however, I hope this still works for you Ziffixture 6913 — 4y
0
I hope all that works out, if so, don't forget to accept and upvote if you like Ziffixture 6913 — 4y
0
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? RobloxGameingStudios 145 — 4y
0
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 RobloxGameingStudios 145 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

0
because u can use a item or kill the human, and view the model/human's stats by clicking it. RobloxGameingStudios 145 — 4y

Answer this question