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

How do I get a variable into two scripts?

Asked by 4 years ago

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?

0
are you trying to get LocalPlayer or other things that is client script only on a local script? MarcTheRubixQb 153 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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!

0
This worked, thank you! walkerking98 2 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

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

remoteEvent.OnServerEvent:Connect(function(mousePos)
0
When I try this it says, "Vector3 expected, got instance." walkerking98 2 — 4y

Answer this question