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

my script is firing a function when it shouldn't?

Asked by 4 years ago
Edited 4 years ago

Sorry for the bad title but i didn't know exactly what to write. i am currently making a script which allows the player to pick up a model by pressing a key if the model is currently not being held by some one else and they are not currently holding a model. i have that part of the script sorted but i need to track if the player should be grabbing or dropping a model when they press "E" i have a system in place changing the string name of an index to determine if the player should send a request to the server to drop the model or pick it up but the code picks up the model and then drops it with one single key press, no errors are displayed and i think its a matter of variable placement and timing, please help. (i haven't made the player physically pick up the models yet i'm currently just working on ownership and making sure things work properly)

this is what get printed out:

 Model successfully claimed!
  canDrop
  Model successfully unclaimed!
  canGrab

Client:

-- Remote variables
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvents = replicatedStorage.RemoteEvents
local remoteFunctions = replicatedStorage.RemoteFunctions
local reqGrab = remoteFunctions.RequestGrab
local stpGrab = remoteEvents.StopGrab
-- Variables
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local Uis = game:GetService("UserInputService")
local character = player.Character or player.CharacterAdded:Wait()
local root = character:FindFirstChild("HumanoidRootPart")
local canRequestGrab = {["grabOrDrop"] = "canGrab"}
local part = nil
-- Requesting to grab or stop grab
Uis.InputBegan:Connect(function(inputObject)
    if inputObject.KeyCode == Enum.KeyCode.E then
        -- If grab
        if canRequestGrab["grabOrDrop"] == "canGrab" then
            if mouse.Target ~= nil then 
                local model = mouse.Target:FindFirstAncestorWhichIsA("Model")
                if model then
                    local primaryPart = model:FindFirstChild("PrimaryPart")
                    if primaryPart then
                        local targetFilter = primaryPart
                        if mouse.Target ~= primaryPart then 
                            part = mouse.Target
                            reqGrab:InvokeServer(part)
                            wait(1)                         
                            canRequestGrab["grabOrDrop"] = "canDrop"
                            print(canRequestGrab["grabOrDrop"])                 
                        end
                    end 
                end 
            end
        end
        -- If Drop
        if canRequestGrab["grabOrDrop"] == "canDrop" then
            stpGrab:FireServer(part)
            part = nil
            wait(1)
            canRequestGrab["grabOrDrop"] = "canGrab"
            print(canRequestGrab["grabOrDrop"])
        end 
    end
end)

Server:

-- Remote variables
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvents = replicatedStorage.RemoteEvents
local remoteFunctions = replicatedStorage.RemoteFunctions
local reqGrab = remoteFunctions.RequestGrab
local stpGrab = remoteEvents.StopGrab
-- Variables
local playerParts = {}
local collectionService = game:GetService("CollectionService")
local tag = "PickUp"
-- Grabs part
function requestGrab(player, part)
    if playerParts[part] ~= nil then return false end
    local model = part:FindFirstAncestorWhichIsA("Model")
    if model then
        if collectionService:HasTag(model, tag) then
            playerParts[part] = player
            print("Model successfully claimed!")
        end
    end 
end
-- Stops grab
function stopGrab(player, part)
    if playerParts[part] ~= player then return false end
    playerParts[part] = nil 
    print("Model successfully unclaimed!")
end
-- Requests a grab
reqGrab.OnServerInvoke = (function(player, part)
    requestGrab(player, part)
end)
-- Requests to stop a grab
stpGrab.OnServerEvent:Connect(function(player, part)
    stopGrab(player, part)
end)

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

I am not exact, but I do recommend waiting for the game to load completely before starting scripts.

Just add a simple way to wait for the game to load, you can either put a wait() or use my recommendation:


repeat wait() until game.Loaded print(Loaded.)

If this doesn't work, feel free to reply and I will look into this further.

~best regards, DetectiveRaie

EDIT: You could also, make sure that all of the local variables are not firing a function, most functions even when placed in a variable, will still fire.

0
this does not fix it but thanks for the recommendation Code1400 75 — 4y
0
I have actually managed to get this script working before and even included the ability to rotate the part but the code was very messy and would bugs would appear in a proper game despite it being glitch less during a local server through studio Code1400 75 — 4y
0
Nvm i figured it out, since the two seperate if statements were in the same input function when the script was done with the first if statement it moved onto the next one Code1400 75 — 4y
Ad

Answer this question