What do you want to achieve? Keep it simple and clear!
I want to make a magnet that pulls in coins in its radius like Magnet Simulator 1 & 2
What is the issue? Include screenshots / videos if possible!
Im sending a remoteEvent to the server when a magnet tool is equipped and thats where i make a hitbox but the touch event for the hitbox doesnt work.
Client:
local char = script.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local rss = game:GetService("ReplicatedStorage")
local magnetEvent = rss.Events.Magnet.Magnet
local tool = plr.Backpack:FindFirstChildOfClass("Tool")
if tool:GetAttribute("Magnet") then
tool.Equipped:Connect(function(mouse)
magnetEvent:FireServer(tool,false)
end)
tool.Unequipped:Connect(function()
magnetEvent:FireServer(tool,true)
end)
end
Server:
local hb = nil
game:GetService("ReplicatedStorage").Events.Magnet.Magnet.OnServerEvent:Connect(function(plr,tool,IsUnequipping)
local char = plr.Character
if IsUnequipping == false then
hb = Instance.new("Part")
hb.Shape = Enum.PartType.Cylinder
hb.Name = "hitbox-"..plr.Name
hb.Parent = workspace.Map.RangeParts
hb.Size = Vector3.new(20,tool:GetAttribute("Range")*2,tool:GetAttribute("Range")*2)
hb.Rotation = Vector3.new(0,0,-90)
hb.Position = char.HumanoidRootPart.Position
hb.Anchored = true
hb.CanCollide = false
hb.Transparency = 0.8
while task.wait(0.1) do
workspace.Map.RangeParts:WaitForChild(hb.Name).Position = char.HumanoidRootPart.Position
end
hb.Touched:Connect(function(hit)
if not hit.Parent.Humanoid then
if hit:GetAttribute("Coin") == true then
print("is coin")
end
end
end)
else
hb:Destroy()
hb = nil
end
end)
The output only gives one error even though said error doesn’t effect the magnet range part at all. It still works as intended
Error:
Sorry for the late reply but try replacing your server script with this:
local hb = nil
game:GetService("ReplicatedStorage").Events.Magnet.Magnet.OnServerEvent:Connect(function(plr,tool,IsUnequipping)
local char = plr.Character
if IsUnequipping == false then
hb = Instance.new("Part")
hb.Shape = Enum.PartType.Cylinder
hb.Name = "hitbox-"..plr.Name
hb.Parent = workspace.Map.RangeParts
hb.Size = Vector3.new(20,tool:GetAttribute("Range")*2,tool:GetAttribute("Range")*2)
hb.Rotation = Vector3.new(0,0,-90)
hb.Position = char.HumanoidRootPart.Position
hb.Anchored = true
hb.CanCollide = false
hb.Transparency = 0.8
while task.wait(0.1) do
workspace.Map.RangeParts:WaitForChild(hb.Name).Position = char.HumanoidRootPart.Position
end
hb.Touched:Connect(function(hit)
if not hit.Parent:FindFirstChild("Humanoid") then
if hit:GetAttribute("Coin") == true then
print("is coin")
end
end
end)
else
hb:Destroy()
hb = nil
end
end)
Sorry for the late reply aswell. Since I thought about the lag it might cause due to this being a tool I decided to rework the script to include the hitbox on the client and only to invoke the server when it gets touched to add to coins leaderstat. This code should still help me figure out whatever the hell im doing.
I personally would do this a different way. Consider using either workspace:GetPartBoundsInRadius() or workspace:GetPartBoundsInBox() (documentation is under WorldRoot [WorldRoot | Documentation - Roblox Creator Hub]). I believe this could be better for your situation. If you need a better explanation I’d be happy to provide it.
That makes the game vulnerable as exploiters can just use FireServer and get free coins, unless you do checks on the server
For the server script put the while loop after the Touched connection, else it won’t work as while loops make the code yield and won’t run any code behind it until the loop’s broken or stopped
You have two options to make it secure. you can either handle it on the client. if you choose to handle it on the client make the checks on the server semi to really strict (also make sure to typecheck the arguments if they are an Instances or not).
I have decided to go on server
guess what: NEW ERROR!
when i say if not hit.Parent.Humanoid Luau is doing is usual thing and completely ignoring that if statement and complaining that Instance.Humanoid does not exist
Edit: Forgot that im stupid and did not change the code to what you suggested