Touch not working correctly

  1. What do I want to achieve?
    I want to make it so the player can deploy a working landmine

  2. What is the issue?
    Landmines that are deployed by a player dont blow up while landmines that are placed in studio do

  3. What solutions have I tried so far?
    I have tried to change the scripts below in many ways, all of them failling to fix this issue


The code that makes the landmines blow up when touched (regular script in model version landmine)
function Touch(hit)
	local Hum = hit.Parent:FindFirstChild("Humanoid")
	Hum.Health = 0
	local explosion = Instance.new("Explosion")
	explosion.BlastRadius = 20
	explosion.ExplosionType = Enum.ExplosionType.Craters -- damages terrain
	explosion.Position = script.Parent.Position
	explosion.Parent = script.Parent
	local explode = Instance.new("Sound")
	local random = Random.new()
	explode.Parent = script.Parent
	explode.SoundId = "rbxassetid://9070368064"
	explode.PlaybackSpeed = random:NextNumber(1,2)
	explode:Play()
	wait(1)
	local Landm = script.Parent.Parent
	Landm:Remove()
end

script.Parent.Touched:connect(Touch)

Code that lets the player deploy the landmine (a local script in the tool version landmine)
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local Tool = script.Parent

Mouse.Icon = "rbxasset://SystemCursors/Wait"

Tool.Equipped:Connect(function()
	Mouse.Button1Down:Connect(function()
		local Landmine2 = RS.CloneLandmine:Clone()
		Landmine2.Parent = game.workspace
		Landmine2.Name = Player.Name.. "'s Landmine"
		Landmine2.Landmine.Position = Mouse.Hit.Position + Vector3.new(0,0.2,0) --33.5
		script.Parent:Destroy()
	end)
end)

Video example of the issue


How this system works just incase:

Landmines blow up when touched
Landmines can be placed by a player if they click the ground while holding the tool version of the landmine

1 Like

It might be because LocalScripts only affect the Client. It may be the case that others won’t be able to see the mine you’ve placed down because the LocalScript put it there so only the Client can see this. This may be the reason as to why the Script does not work.

1 Like

Thats what I was thinking but GetMouse() only works on the client

Use a RemoteEvent and send the position to the server and request the server to put down a mine at that location.

P.S at the time of writing this my roblox studio updated and now i have the new ui noooooo :skull:

1 Like

I recommend you use a remote event.

Also, do not put a function inside of a function because it will make that function over and over again.

Corrected Code

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local Tool = script.Parent

Mouse.Icon = "rbxasset://SystemCursors/Wait"

local isEquipped = false;
Tool.Equipped:Connect(function()
isEquipped  = true;
end)

Tool.Unequipped:Connect(function()
	isEquipped = false;
end)

Mouse.Button1Down:Connect(function()
    if isEquipped  then
		-- Fire a remote to the server. You can not fire the mouse directly, but you can fire the values of the mouse. Such as, position, cframe, etc.
       end
	end)
1 Like

I know how to use RemoteEvents but not send positions with them, could you guess some sort of example?

lmao i got that update a while ago and somehow its gone now, dont think ill ever accept them new icons

1 Like

You need to say FireServer(Mouse.Hit.Position) because you can’t send the Mouse directly.

1 Like

Do what @OwlCodes said, (he kinda stole my solution)

1 Like

I borrowed your words and put it into code.

2 Likes

Thank you so much! One problem I have encounter is receiving the position in the server script, any chance you can show a quick example of that

do this on server script

YourRemoteVariable.OnServerEvent:Connect(function(plr,position) 
       
end)
1 Like

[Client] Firing Mouse Position To Server

local Player = game.Players.LocalPlayer

local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
	script.Parent.RemoteEvent:FireServer(Mouse.Hit.Position)
end)

[Server] Getting Information

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, MousePosition)
	
end)
1 Like

Thank you, @incognitobot_rblx and @kercig for all the help, i really appreciate it :))

1 Like