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

Passing variable through Remote Function not working?

Asked by 3 years ago

I've been trying to make a tool which allows you to mine rocks depending on the level etc. I made a remote function so the Variable of rockhit could go to the server side. I think it is not working because Mouse is something only used on the Client side, I could be wrong though.

Here is my script so far:

Client:

local Pickaxe = script.Parent
local PickaxeHitSound = script.Parent.PickaxeHit
local ToolBreakSound = script.Parent.ToolBreak
local Durability = script.Parent.Durability

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local RemoteEvent = Pickaxe:WaitForChild("RemoteEvent")

local Debounce = true
local Delay = 1

function slash()
    local anim = Instance.new("StringValue")
    anim.Name = "toolanim"
    anim.Value = "Slash"
    anim.Parent = Pickaxe
end


Pickaxe.Activated:Connect(function()
    if Debounce then
        Debounce = false
        slash()

        if Mouse.Target.Name == "CoalRock" then
            local rockhit = Mouse.Target.Parent
            RemoteEvent:FireServer(rockhit)
        elseif Mouse.Target.Name == "BronzeRock" then
            print("Pickaxe too low level")
        elseif Mouse.Target.Name == "GoldRock" then
            print("Pickaxe too low level")
        else
            print("Error")
        end

        wait(Delay)
        Debounce = true
    end
end)

Server:

local Pickaxe = script.Parent
local RemoteEvent = Pickaxe:WaitForChild("RemoteEvent")

RemoteEvent.OnServerEvent:Connect(function(rockhit)
    print(rockhit)
end)

If anybody could help me out that'd be really appreciated.

1 answer

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

OnServerEvent's received parameters are always going to be, first, the player who fired the remote and then all of your other variadic arguments passed to the FireServer function on the client. So your OnServerEvent code should look like:

RemoteEvent.OnServerEvent:Connect(function(player, rockhit)
    print(rockhit)
end)

Also, you mentioning that there may be an issue with the object you've passed over because the mouse is solely a client object does bring up a concern: if the object your mouse is hovering over is created on the client, which means it doesn't exist on the server, or it was destroyed on the server, what the server sees your argument from FireServer is a nil value as its 2nd parameter. Be wary of that.

0
Thank you so much it worked Equals_Sign 4 — 3y
Ad

Answer this question