Need help with a rock breaking script

Hey!

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.
29%20AM
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!

2 Likes

Try changing event:FireEvent() to event:FireServer().

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
1 Like

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.

Could you put a print in the .mouse button down function?

1 Like

I got nothing, so apparently there’s something wrong with the button.

Are you sure there is nothing in the output?

Also, your local script should be a descendant of the screen gui, not workspace?

Local scripts are meant for a client, and only run a descendant of a player object or their character.

If I could see a picture of your screen gui dropdown (object hierarchy), it would help greatly.

Also, a picture of the object hierarchy of ReplicatedStorage would help.

With that information, I could solve this much more easily.

Sorry if I’m bombarding you with questions, It’s just there are many possible causes of this problem

1 Like

I just moved the LocalScript to where you said and tested with no result. Here is a screenshot of the hierarchy of both requested places:
48%20AM 46%20AM
Also, the only output I have is from my plugins, nothing from print functions.

This is a post that is on the devforums.

I just did that with REALTimothy0812, and how can I solve the button issue?

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)
1 Like

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.

1 Like

It works perfectly! Thank you so much!

1 Like

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.

Have a good day.

1 Like

You did! Thanks for the tip! Have a great day to you as well.

1 Like