on the left one, in the lecal scrip code in line 22 there is aproblem with mouse, look…
- User
on the left one, in the lecal scrip code in line 22 there is aproblem with mouse, look likes roblox dosnt accapt it, can you replace it with something that works?
- Assistant
Here’s a drop-in replacement for the LocalScript that fixes the undefined
mousereference and swaps the deprecatedRay.newcall for the modernworkspace:RaycastAPI.Replace the entire LocalScript with the snippet below (or just copy-paste the code part you need).
Everything else in your place can stay the same.---------------------------------------------------- -- SlapTool ▸ LocalScript (client-side logic) ---------------------------------------------------- local tool = script.Parent local player = game.Players.LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local humanoid = character:WaitForChild("Humanoid") local remoteEvent = tool:WaitForChild("SlapEvent") -- Animation setup local anim = Instance.new("Animation") anim.AnimationId = "rbxassetid://YOUR_ANIMATION_ID" -- <— replace local slapAnim = humanoid:LoadAnimation(anim) -- Variables local mouse -- we’ll grab this when the tool is equipped local cooldown = false local COOLDOWN_T = 1.5 -- seconds local RANGE = 10 -- studs ---------------------------------------------------- -- Helper: play SFX if it exists ---------------------------------------------------- local function playSlapSFX() local sfx = tool.Handle:FindFirstChildOfClass("Sound") if sfx then sfx:Play() end end ---------------------------------------------------- -- Equip: cache the player’s mouse ---------------------------------------------------- tool.Equipped:Connect(function() mouse = player:GetMouse() -- mouse is now valid every time the tool is equipped end) ---------------------------------------------------- -- Activated: perform slap ---------------------------------------------------- tool.Activated:Connect(function() if cooldown then return end cooldown = true -- Animation & SFX slapAnim:Play() playSlapSFX() -------------------------------------------- -- Raycast to find what we’re hitting -------------------------------------------- if mouse and mouse.Hit then local origin = tool.Handle.Position local direction = (mouse.Hit.Position - origin).Unit * RANGE -- Ignore the player’s own character local params = RaycastParams.new() params.FilterDescendantsInstances = {character} params.FilterType = Enum.RaycastFilterType.Blacklist local result = workspace:Raycast(origin, direction, params) if result and result.Instance then local targetModel = result.Instance:FindFirstAncestorOfClass("Model") if targetModel and targetModel:FindFirstChild("Humanoid") then remoteEvent:FireServer(targetModel) end end end -- Simple cooldown task.wait(COOLDOWN_T) cooldown = false end)What changed & why
-
mouseis now obtained insidetool.Equipped, guaranteeing it’s valid whenever the tool is held.
•local mouse = player:GetMouse()only works in a LocalScript, so this is fine on the client. -
Deprecated
Ray.newwas removed.
• Modern Roblox usesworkspace:RaycastwithRaycastParamsfor cleaner filtering. -
Added
RANGEandCOOLDOWN_Tconstants to tweak slap distance and cooldown easily. -
Added a quick helper (
playSlapSFX) to avoid errors if the sound is missing.
That’s it—drop this script in, swap
YOUR_ANIMATION_ID, and your slap tool should work with no more “mouse is nil” errors. Let me know if you need any other tweaks or features! 🖐️💥 -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.