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

Error when making a possession script, isnt reaching the second OnServerInvoke?

Asked by 2 years ago

So I have this possession script, its activated when clicking on a player and you possess their character but as a result you lose your original character so theres an empty clone there for you to inhabit after you depossess by pressing R when youre in the other players possessed body, the depossession phase i cant reach though because i have a strange error but first im gonna show all the scripts

localscript in starterplayerscripts


local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local UserInputService = game:GetService("UserInputService") local Player = Players.LocalPlayer local Mouse = Player:GetMouse() UserInputService.InputEnded:Connect(function(input) if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.R then ReplicatedStorage.ReturnPossession:InvokeServer() end if input.UserInputType ~= Enum.UserInputType.MouseButton1 and Mouse.Target then local targetPlayer = Players:GetPlayerFromCharacter(Mouse.Target.Parent) if not targetPlayer then return end local result = ReplicatedStorage.TakePossession:InvokeServer(targetPlayer) if result then print(result.Error) end end end) ReplicatedStorage.EnableClientControls.OnClientEvent:Connect(function(enable) local controls = require(Player.PlayerScripts.PlayerModule):GetControls() if enable then controls:Enable(true) else controls:Disable() end end)

serverscript in serverscriptservice

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local IS_POSSESSED_KEY = "IsPossessed"
local IS_POSSESSING_KEY = "IsPossessing"
local RETURN_CHARACTER_KEY = "ReturnCharacter"

function updateReturnToCharacterValue(character, parent)
    local value = nil
    if parent:FindFirstChild(RETURN_CHARACTER_KEY) then
        value = parent[RETURN_CHARACTER_KEY]
    else
        value = Instance.new("ObjectValue")
        value.Name = RETURN_CHARACTER_KEY
        value.Parent = parent
    end
    value.Value = character
end

ReplicatedStorage.TakePossession.OnServerInvoke = function(player, target)
    if not target:IsA("Player") then
        return {
            Error = "Target is not a player"
        }
    end

    if player == target then
        return {
            Error = "You cannot possess yourself"
        }
    end

    if player:GetAttribute(IS_POSSESSED_KEY) then
        return {
            Error = "You are already possessed"
        }
    end

    if player:GetAttribute(IS_POSSESSING_KEY) then
        return {
            Error = "You are already possessing someone else"
        }
    end

    if target:GetAttribute(IS_POSSESSED_KEY) then
        return {
            Error = target.DisplayName .. " is already possessed"
        }
    end

    if target:GetAttribute(IS_POSSESSING_KEY) then
        return {
            Error = target.DisplayName .. " is already possessing someone else"
        }
    end

    -- Clone original character and reassign character
    -- (clone is required because character is deleted after Character reassignment)
    local originalCharacter = player.Character
    originalCharacter.Archivable = true
    local originalCharacterClone = originalCharacter:Clone()
    originalCharacterClone.Parent = originalCharacter.Parent
    updateReturnToCharacterValue(originalCharacterClone, player)

    -- Assume ownership of the target
    local targetCharacter = target.Character
    targetCharacter.HumanoidRootPart:SetNetworkOwner(player)
    player.Character = targetCharacter

    -- Tell the posessed player to seize controls
    ReplicatedStorage.EnableClientControls:FireClient(target, false)


    --------RETURN POSSESSION
    game.ReplicatedStorage.ReturnPossession.OnServerInvoke()

    --possessed player receives back controls
    ReplicatedStorage.EnableClientControls:FireClient(target, true)

    --Possessors ownership of target is revoked 
    player.Character = player[RETURN_CHARACTER_KEY].Value




    -- Set info on player and target
    player:SetAttribute(IS_POSSESSED_KEY, false)
    player:SetAttribute(IS_POSSESSING_KEY, true)

    target:SetAttribute(IS_POSSESSED_KEY, true)
    target:SetAttribute(IS_POSSESSING_KEY, false)
end

now the error is "OnServerInvoke is a callback member of RemoteFunction; you can only set the callback value, get is not available."

and it says this error on line 19 in the localscript

local result = ReplicatedStorage.TakePossession:InvokeServer(targetPlayer)

No idea what it could possibly mean but its preventing my goal of the player being able to depossess

1 answer

Log in to vote
0
Answered by 2 years ago

Hiya o/

Your code looks fine, one thing I noticed is your not returning anything if the server has finished the invoke successfully.

0
hmmm??? what should i return exactly??? THEGOBLINTOPLAD 0 — 2y
0
hmmm??? what should i return exactly??? THEGOBLINTOPLAD 0 — 2y
0
Maybe return something like true loowa_yawn 383 — 2y
Ad

Answer this question