Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I get a variable into two scripts?

I'm attempting to create a tool that will explode wherever the user clicks. But I can't get the variable that tells where the player clicks into the 2nd script that actually makes the explosion.

I have 2 scripts, the first one being a Local Script which has this code in it

wait(1)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("Explode")
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
local handle = script.Parent:WaitForChild("Handle")
local tool = script.Parent
local mouse = player:GetMouse()
local mousePos = mouse.Hit.p

mouse.Button1Down:Connect(function()
	if tool.Activated then
		local mousePos = mouse.Hit.p
		remoteEvent:FireServer()
	end
end)

The 2nd script being a regular script that actually creates the explosion so the explosion isn't client sided.

wait(1)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("Explode")

remoteEvent.OnServerEvent:Connect(function()
	local explosion = Instance.new("Explosion")
	explosion.BlastRadius = 30
	explosion.ExplosionType = Enum.ExplosionType.NoCraters -- damages terrain
	explosion.Position = mousePos
	explosion.Parent = script.Parent
	explosion.DestroyJointRadiusPercent = 100
	explosion.BlastPressure = 400000
end)

I have tried using Global Variables, I have tried using stored values, but nothing I do seems to have worked resulting in frustrating errors. How would I fix this?

1 comment

0

are you trying to get LocalPlayer or other things that is client script only on a local script?

2 answers

✓ Accepted answer

To get a variable into the localscript and the script, do

remoteEvent:FireServer(mousePos)

in the local script. And receive it on the server as

remoteEvent.OnServerEvent(player, mousePos)

because first argument in .OnServerEvent() is always the player instance the server event came from. The second argument is the parameter you fired in :FireServer(). Hope this helps!

1 comment

0

This worked, thank you!

This error is easy to solve, just make these two adjustments.

local mousePos = mouse.Hit.p
remoteEvent:FireServer(mousePos)

remoteEvent.OnServerEvent:Connect(function(mousePos)

1 comment

0

When I try this it says, "Vector3 expected, got instance."

This discussion is preserved as it was. New answers, comments, and votes are closed.