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

Every Time I Click Once the Remote Event Runs Twice for Some Reason?

Asked by 6 years ago
Edited 6 years ago

So I have a tool that a user holds when they equip it, and if a they press on a certain part, a boat should spawn at the coordinate the mouse was pressed on for said part. I use a remote event to spawn the boat model to the server based on client side request.

Here is the local script, followed by the server script:

local ReplicatedStorage= game:GetService("ReplicatedStorage")
local createPlaceBuildingEvent= ReplicatedStorage:WaitForChild("CreatePlaceBuildingEvent")

tool= script.Parent
handle= tool:WaitForChild("Handle")
--enabled= true
--game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

local Plr= game.Players.LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:wait()
while game.Players.LocalPlayer.Character.Parent == nil do
    game.Players.LocalPlayer.Character.AncestryChanged:wait()
end

--local Player= game.Players.LocalPlayer
local Mouse= Plr:GetMouse()

local animTrack = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent.slashDown)

script.Parent.CanBeDropped= false
local enabled= false
local equipped= false

tool.Equipped:connect(function()
--equipped= true
end)

local count= 0

tool.Activated:connect(function()
    animTrack:Play()
    --game.StarterGui.checkBoatBuilt.Value= 1
    --print("v pressed")
    if not Mouse.Target then
        return
    end
    if tostring(Mouse.Target)== "Seafloorfloorsea" and script.Parent.Name~= "Junk" then
        local mouseHitX= Mouse.Hit.X
        local mouseHitY= Mouse.Hit.Y
        local mouseHitZ= Mouse.Hit.Z
        local toolValue= script.Parent.isTool.Value
        local count= 0
        createPlaceBuildingEvent:FireServer(mouseHitX,mouseHitY,mouseHitZ,count)
        --game.Players.LocalPlayer.PlayerGui.ToolboxGui:FindFirstChild("frame"..tostring(script.Parent.isTool.Value)).TextButton:Destroy()
        --script.Parent= game.ServerStorage.trashCan
        --script.Parent:Destroy()
        game.Players.LocalPlayer.PlayerGui.ToolboxGui:FindFirstChild("frame"..tostring(script.Parent.isTool.Value)).TextButton.Text= "Junk"
        script.Parent.Name= "Junk"
        script.Parent.Handle.BrickColor= BrickColor.Black()
        count= count+1
        --print(count)
        --enabled= false
        --script.Parent= nil
    end

end)

tool.Unequipped:connect(function()
--equipped= false
end)

tool.Deactivated:connect(function()
    animTrack:Stop()
end)

Next:

--this script places buildings on the ws from tools
local ReplicatedStorage= game:GetService("ReplicatedStorage")
local createPlaceBuildingEvent= Instance.new("RemoteEvent",ReplicatedStorage)
createPlaceBuildingEvent.Name= "CreatePlaceBuildingEvent"

--local count= 0--debounce
--local enabled= false

function onToolToBuilding(sender,mouseHitX,mouseHitY,mouseHitZ)
    if workspace.checkBoatBuilt.Value== 0 then
        workspace.checkBoatBuilt.Value= 1
        --enabled= true
        --local cloneSmallBoat= workspace.AdminRoom["Small Boat"]:Clone()
        local cloneSmallBoat= workspace["Small Boat Master"]:Clone()
        cloneSmallBoat.Name= "Small Boat"
        --local cloneSmallBoat= workspace["Raft"]:Clone()   
        cloneSmallBoat.Parent= workspace

        for index,descandant in pairs(cloneSmallBoat:GetDescendants()) do
            if descandant:IsA("Part") then
                descandant.CanCollide= true
                descandant.Transparency= 0
                if descandant.Name== "MainPart" then
                    descandant.BottomSurface= "Smooth"
                end
            elseif descandant:IsA("VehicleSeat") then
                descandant.Disabled= false
                descandant.CanCollide= true
                descandant.Transparency= 0
            elseif descandant:IsA("ClickDetector") then
                descandant.MaxActivationDistance= 32
            end
        end
        --]]
        cloneSmallBoat:MakeJoints()
        cloneSmallBoat:MoveTo(Vector3.new(mouseHitX,23,mouseHitZ))
        --count= count+1
        --print(count)
        --[[
        if count== 1 then
            enabled= true
        elseif count== 2 then
            enabled= false
        end
        --]]
    end--if enabled

end
createPlaceBuildingEvent.OnServerEvent:Connect(onToolToBuilding)

As you can see in the commented areas I edited my script by using the variable count as an attempt to count how many boats spawn at a time; if two boats spawn within a short time then it should delete one of the boats. This works the first time, and the first time only. After that, every mouse click spawns two boats again (though at least by printing out the variable I find the the event runs twice). I only want one boat to spawn for each single mouse click.

I would be willing to provide any further clarifications regarding my code, and may play around with debounce in the meantime.

EDIT: to add more info it it would help: this happens both on Studio and online, without any errors. When the boats spawn one boat spawns right on top of the other. I made the bottom side of the main part smooth so that I could at least move the boat on top off of the one below it, but is not a good quick fix.

2 answers

Log in to vote
1
Answered by 6 years ago

The answer here is simple. You need to add a debounce so tool activation isn't registered more than once upon a click. If you add a debounce, you can also remove your system that detects whether or not two boats have spawned. I will add an example of a debounce below.

local Tool = script.Parent
local Debounce = true
Tool.Activated:Connect(function()
if Debounce == true then
Debounce = false
print ("Hello!")
wait(1) -- You can change this to any delay you want. wait(1) will make the player wait a second before they can activate the function again.
Debounce = true
end
end)

By doing something like this to your current script, you can make sure that the function doesn't run more than once when a player clicks. If you don't understand or want to learn more about debounce, try looking here. Good luck! https://wiki.roblox.com/index.php?title=Debounce

Ad
Log in to vote
0
Answered by 6 years ago

You passed the mouses hit as a parameter , it errors because the server doesn't have a mouse while the client does.

0
I can kind of see what you're saying, assuming you're talking about error as in there's no mouse on the server side and not as in red error in the output box. Are you suggesting that I should indirectly pass the mouse information? Houlardy642 28 — 6y

Answer this question