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

Possession script wont acknowledge my remoteEvent?

Asked by 2 years ago

I genuinely dont know how this isnt working, im getting no errors whatsoever and it refuses to reach the remoteEvent "returnPossession" it depends on whether the player presses the R key obviously and its disconnected from the remoteFunction "TakePossession" that which ACTUALLY works. I'm at my wits end here I do not know how to fix this at all.

local script

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 game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.C) 
        and Mouse.Target ~= nil and Mouse.Target.Parent.Name ~= "Workspace" 
        and Mouse.Target.Parent:FindFirstChildOfClass("Humanoid") ~= nil then
    local targetPlayer = Players:GetPlayerFromCharacter(Mouse.Target.Parent)
    print("targetplayer1")
    if not targetPlayer then
        print("target player not found")
    end
    local result = replicatedStorage.TakePossession:InvokeServer(targetPlayer)
    print("possession has been fired")
    if result then
        print(result)
        print("result has passed")
    end

    end--end of function
end) ---end of function

replicatedStorage.EnableClientControls.OnClientEvent:Connect(function(enable)
    local controls = require(Player.PlayerScripts.PlayerModule):GetControls()
    if enable then
        controls:Enable(true)
        print("controls enabled")
    else
        controls:Disable()
        print("controls disabled")
    end
end)

--NEW R TO UNPOSSESS SCRIPT
--replicatedStorage.ReturnPossession.OnClientEvent:Connect(function(returned)
    --print("UNPOSSESS PRINT")
--end)


--OLD R TO UNPOSSESS SCRIPT
if userInputService:IsKeyDown(Enum.KeyCode.R) then
    replicatedStorage.ReturnPossession:FireServer()
    print("depossession server fired")
end

server/receiver

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)
    print("possession fired has reached server")
    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
    print("possessors body has been cloned")
    updateReturnToCharacterValue(originalCharacterClone, player)

    -- Assume ownership of the target
    local targetCharacter = target.Character
    targetCharacter.HumanoidRootPart:SetNetworkOwner(player)
    player.Character = targetCharacter
    print("targets body has been possessed")

    -- Tell the posessed player to seize controls
    ReplicatedStorage.EnableClientControls:FireClient(target, false)
    print("targets body has lost its controls")

    -- Set info on player and target
    player:SetAttribute(IS_POSSESSED_KEY, false)
    player:SetAttribute(IS_POSSESSING_KEY, true)
    print("first attributes have been set")

    target:SetAttribute(IS_POSSESSED_KEY, true)
    target:SetAttribute(IS_POSSESSING_KEY, false)
    print("second attributes have been set")
    end

local function Depossess(player, target)
    print("depossess fired has reached the server")
    ReplicatedStorage.EnableClientControls:FireClient(target, true)
    print("target received controls back")

    player.Character = player[RETURN_CHARACTER_KEY].Value
    print("possessor is now in body of their clone")
end

    --return to body 
ReplicatedStorage.ReturnPossession.OnServerEvent:Connect(Depossess)

contact me via discord: fenti#8587

Answer this question