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

How do you convert a Client - Client Event to a Server - Client?

Asked by 4 years ago

So im making something for when you press E on an object it will pick it up but if i pick up all of the objects then my friend in the server can still pick up the same ones as i picked up im thinking its something to do with that i have a Bindable Event for communicating between two local scripts. I think the fix will be to convert one of the scripts to a server but i dont know which one or how.

First Script :

local UIS = game:GetService("UserInputService")
local pickupHotkey = "E"

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local PlayerGUI = plr:WaitForChild("PlayerGui")
local PickupGUI = PlayerGUI:WaitForChild("PickUpGUI")

local Slot1EventWood = game.ReplicatedStorage.Slot1Wood

UIS.InputChanged:Connect(function(input)
    if mouse.Target then
    if mouse.Target:FindFirstChild("Pickup") then
       local item = mouse.Target
       PickupGUI.Adornee = item
       PickupGUI.ObjectName.Text = item.Name
       PickupGUI.Enabled = true
    else
        PickupGUI.Adornee = nil
        PickupGUI.Enabled = false
    end
    end
end)

UIS.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode[pickupHotkey] then
    if mouse.Target then
        if mouse.Target:FindFirstChild("Pickup")then
        local item = mouse.Target
        if item then
            local distanceFromItem = plr:DistanceFromCharacter(item.Position)
            if distanceFromItem < 20 then
                if item.Name == "Wood" then
                    Slot1EventWood:FireServer()
                end
            end
        end
        end 
    end
    end
end)

Second script :

--//Variables\\--
local plr = game.Players.LocalPlayer
local Slot1EventWood = game.ReplicatedStorage.Slot1Wood
local mouse = plr:GetMouse()
local item = mouse.Target

Slot1EventWood.Event:Connect(function()
    print("Client Received the Event")
    if script.Parent.QuantitiyVal == 1 then
        print("No room >:)")
        if script.Parent.Item == "Wood" then
            print("Room due to stacking")
        elseif script.Parent.QuantitiyVal == 99 then
            print("No room due to max stack limit")
        end 
    else
        if item.Name == "Wood" then
            item:Destroy()
        end
        print("Room")
        script.Parent.Item.Value = "Wood"
        script.Parent.QuantitiyVal.Value = script.Parent.QuantitiyVal.Value + 1
        script.Parent.Visible = true
        script.Parent.Quantity.Text = script.Parent.QuantitiyVal.Value
    end
end)

2 answers

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

Okay, so the item is the thing you want to destroy on the server? if so doing it in a local script wont work as it's only destroying it for the client. Anyway, you should use a RemoteEvent to fire the server from the local script then copy the code from the second script and paste it into a Server Script, and use Event.OnServerEvent()

First Script (Local Script):

local UIS = game:GetService("UserInputService")
local pickupHotkey = "E"

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local PlayerGUI = plr:WaitForChild("PlayerGui")
local PickupGUI = PlayerGUI:WaitForChild("PickUpGUI") 

local Slot1EventWood = game.ReplicatedStorage.Slot1Wood

UIS.InputChanged:Connect(function(input)
    if mouse.Target then
    if mouse.Target:FindFirstChild("Pickup") then
        local item = mouse.Target
        PickupGUI.Adornee = item
        PickupGUI.ObjectName.Text = item.Name
        PickupGUI.Enabled = true
    else
        PickupGUI.Adornee = nil
        PickupGUI.Enabled = false
    end
    end
end)

UIS.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode[pickupHotkey] then
    if mouse.Target then
        if mouse.Target:FindFirstChild("Pickup")then
        local item = mouse.Target
        if item then
            local distanceFromItem = plr:DistanceFromCharacter(item.Position)
            if distanceFromItem < 20 then
                if item.Name == "Wood" then
                    Slot1EventWood:FireServer()
                end
            end
        end
        end
    end
    end
end)

Second Script (Server Script):


local Slot1EventWood = game.ReplicatedStorage.Slot1Wood Slot1EventWood.OnServerEvent:Connect(function(plr) local mouse = plr:GetMouse() repeat wait() until mouse.Target ~= nil local item = mouse.Target print("Client Received the Event") if script.Parent.QuantitiyVal == 1 then print("No room >:)") if script.Parent.Item == "Wood" then print("Room due to stacking") elseif script.Parent.QuantitiyVal == 99 then print("No room due to max stack limit") end else if item.Name == "Wood" then item:Destroy() end print("Room") script.Parent.Item.Value = "Wood" script.Parent.QuantitiyVal.Value = script.Parent.QuantitiyVal.Value + 1 script.Parent.Visible = true script.Parent.Quantity.Text = script.Parent.QuantitiyVal.Value end end)

Hopefully this works. If this isn't what you were asking for just post a comment below and I'll try to help you.

0
Theres an error message saying : 09:43:30.008 - Players.EhEhEhEhLPKJ.PlayerGui.InventoryGUI.MainFrame.Slot1.Script:5: attempt to index nil with 'Target' EhEhEhEhLPKJ 9 — 4y
0
Edited the code above. Thank you @iuclds for correcting me. the "local mouse = plr:GetMouse()" needs to be inside the Event though otherwise you'll get an error. xInfinityBear 1777 — 4y
0
It seems to still be saying the same thing just this time for line 7 EhEhEhEhLPKJ 9 — 4y
0
@xInfinityBear EhEhEhEhLPKJ 9 — 4y
View all comments (6 more)
0
You could try a repeat wait as well, don't know if it will work, but i guess you could try it. I'll edit the code above. xInfinityBear 1777 — 4y
0
Just edited my answer, hopefully it works. xInfinityBear 1777 — 4y
0
:( sorry it still doesn't EhEhEhEhLPKJ 9 — 4y
0
I could try make a separate answer to do with that bug EhEhEhEhLPKJ 9 — 4y
0
Damn dude, that sucks. I can't think of anything else, so yea make a separate answer with that issue and hopefully someone helps you with it. xInfinityBear 1777 — 4y
0
alr EhEhEhEhLPKJ 9 — 4y
Ad
Log in to vote
0
Answered by
iuclds 720 Moderation Voter
4 years ago

for xInfinityBear

local Slot1EventWood = game.ReplicatedStorage.Slot1Wood
local mouse = plr:GetMouse()
Slot1EventWood.OnServerEvent:Connect(function(plr)
    if mouse.Target == nil then return end

    local item = mouse.Target

    print("Client Received the Event")
        if script.Parent.QuantitiyVal == 1 then
            print("No room >:)")
            if script.Parent.Item == "Wood" then
                print("Room due to stacking")
            elseif script.Parent.QuantitiyVal == 99 then
                print("No room due to max stack limit")
            end
        else
            if item.Name == "Wood" then
                item:Destroy()
            end
            print("Room")
            script.Parent.Item.Value = "Wood"
            script.Parent.QuantitiyVal.Value = script.Parent.QuantitiyVal.Value + 1
            script.Parent.Visible = true
            script.Parent.Quantity.Text = script.Parent.QuantitiyVal.Value
        end
    end)
0
09:59:21.897 - Players.EhEhEhEhLPKJ.PlayerGui.InventoryGUI.MainFrame.Slot1.Wood:3: attempt to index nil with 'GetMouse' EhEhEhEhLPKJ 9 — 4y
0
It umm still isn't working btw EhEhEhEhLPKJ 9 — 4y
0
@EhEhEhEhLPKJ Look at my comment above on my post. xInfinityBear 1777 — 4y
0
Sorry to bother you again @xInfinityBear another error when i press E on the object : 10:33:46.655 - Players.EhEhEhEhLPKJ.PlayerGui.InventoryGUI.MainFrame.Slot1.Wood:7: attempt to index nil with 'Target' EhEhEhEhLPKJ 9 — 4y
0
@EhEhEhEhLPKJ Edited my code above again, try that. Also make sure to reply to my answer so I can get the notification. xInfinityBear 1777 — 4y

Answer this question