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

How can I fix the remote event script? [Fast]

Asked by 6 years ago
Edited 6 years ago

I've made a script that will fire a remote event whenever i click the button

local event = game.ReplicatedStorage.RemoteEvent -- Location of your event
local price = script.Parent.Parent.Price
local tools = game:GetService("ReplicatedStorage"):WaitForChild("Tools")
local tool = tools:FindFirstChild(script.Parent.Parent.ItemName.Value)
local player = script.Parent.Parent.Parent.Parent.Parent.Parent

script.Parent.MouseButton1Click:Connect(function()

 event:FireServer(player,price,tool)


    end)

and a on server event script

local event = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- Location of your event

event.OnServerEvent:Connect(function(plr,price,tool) -- On event, get player and mouse hit position and arg
      local stats = plr:WaitForChild("leaderstats")
      local Point = stats:WaitForChild("Point")

    local Value = Point.Value
    print(Value)
        Value = Value - price.Value
        print("Done!")


end)

it can print the point of the player but it says value is not a valid member of Player

0
i posted a answer for fix all problems. yHasteeD 1819 — 6y

1 answer

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
6 years ago
Edited 5 years ago

For first, you can't remove value with variable, only update the variable not the value in object,

Example:

-- Default value: 2
local myValue = workspace.myValue.Value -- A IntValue
myValue = myValue - 1
-- Object value: 2, Variable value: 1

Its simple to fix, only use .Value on remove example:

-- Default value: 2
local myValue = workspace.myValue
myValue.Value = myValue.Value - 1
-- Object value now: 1, Variable value: no variable of the value.

(for see more of the .Value see the answer of Incapaxian, you can understand more: Incapaxian Answer)

Fixed scripts:

On your client, only use game.Players.LocalPlayer, also for fire RemoteEvent you dont need to send player, the first argument of the RemoteEvent is the PLayer

local event = game.ReplicatedStorage.RemoteEvent -- Location of your event
local price = script.Parent.Parent.Price
local tools = game:GetService("ReplicatedStorage"):WaitForChild("Tools")
local tool = tools:FindFirstChild(script.Parent.Parent.ItemName.Value)
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    event:FireServer(price.Value, tool.Name) -- Only send price with value, not object and toolName here.
end)

In your event script put this:

local event = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- Location of your event

event.OnServerEvent:Connect(function(plr,price,toolName) 
    local stats = plr:WaitForChild("leaderstats")
    local Point = stats:WaitForChild("Point")

    local tool = game:GetService("ReplicatedStorage"):WaitForChild("Tools"):FindFirstChild(toolName) -- Tool location

    print(Point.Value)
    if Point.Value >= price then
        Point.Value = Point.Value - price
    else
        print("You dont have money.")
    end
    print("Done!")
end)

Well this remote event is not protected so I would recommend you create the price inside the tools and check if the player has the money on the server.

For use protected RemoteEvents only put the Price(with IntValue named as Price) inside the tool and only send the name of tool to server

(Client)

local event = game.ReplicatedStorage.RemoteEvent -- Location of your event
local tools = game:GetService("ReplicatedStorage"):WaitForChild("Tools")
local tool = tools:FindFirstChild(script.Parent.Parent.ItemName.Value)
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    event:FireServer(tool.Name) -- Only send toolName here.
end)

(Server)

local event = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- Location of your event

event.OnServerEvent:Connect(function(plr,toolName) 
    local stats = plr:WaitForChild("leaderstats")
    local Point = stats:WaitForChild("Point")

    local tool = game:GetService("ReplicatedStorage"):WaitForChild("Tools"):FindFirstChild(toolName) -- Tool location
    local price = tool:FindFirstChild("Price")  
    if price then
        print(Point.Value)
        if Point.Value >= price.Value then
            Point.Value = Point.Value - price
        else
            print("You dont have money.")
        end
        print("Done!")
    else
        print("Price not found")
    end
end)

Hope it helped :D

Wiki pages

RemoteEvents/Functions


Errors? tell-me on comments.

0
love ya iamthepurpleguy10 15 — 6y
0
so now I must add a clone line between line 12 and line 13? iamthepurpleguy10 15 — 6y
Ad

Answer this question