I’m trying to make a rock that I modeled sperate into different pieces as if it is being broken. I already have different pieces modeled and imported to Studio, but I need a script that sends the pieces flinging in different directions when a function is called. I’ve already attempted to write a script for this, using Velocity properties and Random() but I am not very experienced in scripting, so it came to no avail. I hope you guys can help!
I wrote a Local and a Server script, the LocalScript is a child of the Workspace, and the Server script is a child of the Rock model.
Here is the LocalScript:
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage:WaitForChild("Event")
local button = game.StarterGui.ScreenGui.TextButton
button.MouseButton1Click:Connect(function()
event:FireEvent()
end)
Here is the Server script:
local rock = workspace.Rock
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage:WaitForChild("Event")
local function breakup(plr)
print("test")
for i,v in pairs(rock:GetChildren()) do
v.Anchored = false
v.Velocity.X = Random(1,5)
v.Velocity.Y = Random(1,5)
v.Velocity.Z = Random(1,5)
end
end
Right now, I don’t know if the scripts work or not, but from my testing, I can’t get the LocalScript to communicate with the server script, or at least I don’t think I can. My output doesn’t give me anything, and the Script Analysis window doesn’t tell me anything.
Also, I am new to posting on the DevForum, if I’m breaking a rule or posting in the wrong place please tell me!
Thanks, guys!
First of all, is event a remote event or remote function. Either way, it should be :FireServer or :Fire.
Also, make sure breakup is connected to the event properly. You can test this with a simple print.
--If event is a remote event
--Client
event:FireServer()
--Server
event.OnServerEvent:Connect(breakup)
Secondly, velocities are a vector 3. Try using this breakup function instead:
local function breakup(plr)
print("test")
for i,v in pairs(rock:GetChildren()) do
v.Anchored = false
v.Velocity = Vector3.new(math.random(1,5), math.random(1,5),math.random(1,5))
end
end
It is a RemoteEvent, and I took your advice but I still got nothing in the output. Here is the server script now:
local rock = workspace.Rock
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage:WaitForChild("Event")
local function breakup(plr)
print("test")
for i,v in pairs(rock:GetChildren()) do
v.Anchored = false
v.Velocity = Vector3.new(math.random(1,5), math.random(1,5),math.random(1,5))
end
end
event.OnServerEvent:Connect(breakup)
And I corrected FireEvent() to FireServer() in the LocalScript.
Oh, I found it. You use StarterGui, instead of the current player’s gui. This means that the button function will never be fired. Try this new button code:
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage:WaitForChild("Event")
local button = script.Parent:WaitForChild('TextButton') --Reference the parent of the local script, and wait for the first object named TextButton to be loaded
button.MouseButton1Click:Connect(function()
event:FireServer()
end)
Good news! The “test” print from the Server script is working with the script you sent, but I get an error telling me “Anchored is not a valid member of Script” in the output.
Oh, the for i,v loop runs over every child, so make sure it only runs on parts:
local rock = script.Parent --Nicer than workspace.Rock
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage:WaitForChild("Event")
local function breakup(plr)
print("test")
for i,v in pairs(rock:GetChildren()) do
if v:IsA('BasePart') then --Run only on parts
v.Anchored = false
v.Velocity = Vector3.new(math.random(1,5), math.random(1,5),math.random(1,5))
end
end
end
event.OnServerEvent:Connect(breakup)
This code will work as long as you have several anchored parts in the Rock model.
Glad it does! If you want the rocks to fire farther, or to change the distance, change the 1 and 5 to a different min a max. Glad I could help, and I hope I was able to teach you a few helpful things about client-server communication.