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?
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!
This error is easy to solve, just make these two adjustments.
local mousePos = mouse.Hit.p remoteEvent:FireServer(mousePos)
remoteEvent.OnServerEvent:Connect(function(mousePos)