Ad
Log in to vote
0

So Im trying to make a Button which lets you spawn but it is doesnt work. What should I do?

Asked by 1 year ago
Edited 1 year ago

I am making a FFA/TDM game where players can enter the game by pressing a button. The button is inside a ScreenGui which is inside the StarterGui. The script inside the button is a localscript

Code (LocalScript):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remfunction = ReplicatedStorage:WaitForChild( "Spawn")
script.Parent.MouseButton1Click:Connect(function()
    local Spawn = remfunction:InvokeServer()
    print("You have been teleported")
end)

ServerCode (On recieving command)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remfunction = ReplicatedStorage:WaitForChild("Spawn")
local function Deploy (player)
    print (player.Name.."wants to deploy")
    local status = game.ServerStorage.GameState
    print("variables defined")
    local character = player.Character
    status:GetPropertyChangedSignal("Value"):Connect(function()
        if status.Value == "1" then
            local ServerStorage = game:GetService("ServerStorage")
            local map = game:GetService("ServerStorage").CurrentMap.Value
            local spawnpoints = map:FindFirstChild("SpawnPoints")
            local AvailableSpawnPoints = spawnpoints:GetChildren()
            print("Teleporting function started")
            character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[math.random(1,#AvailableSpawnPoints)].CFrame + Vector3.new(0,2,0)
            wait(2)
            print("teleported to spawn")

            --Give weapon
            local equipped = game.ServerStorage.PlayerData[player.Name].Equipped
            if equipped.Value ~= "" then
                local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
                weapon.Parent = player.Backpack
            else
                local Sword = ServerStorage.Sword:Clone()
                Sword.Parent = player.Backpack
            end
            print("weapon given")
            local GameTag = Instance.new("BoolValue")
            GameTag.Name = "GameTag"
            GameTag.Parent = character
            print("tag given")
            for _, plr in pairs(player) do
                if not plr.Team then
                    local low = 9999
                    local lowTeam
                    for _, team in pairs(game:GetService('Teams'):GetChildren()) do
                        if #team:GetPlayers()<low then
                            low = #team:GetPlayers()
                            lowTeam = team
                        end
                    end
                    plr.Team = lowTeam
                end
            end
        print("placed in a team")
        print("good to go,start playing!")
    else
        print("you are in lobby stay there until the timer reaches zero :D")
        end
    end)
end

remfunction.OnServerInvoke = Deploy()

Output: ServerScriptService.SpawnScript:4: attempt to index local 'player' (a nil value)

Note: The value of GameState changes to 1 every second until the timer ends

The value of GameState switches to 0 after the timer ends

Server Code:

for i = Gamelength,0,-1 do
            game.ServerStorage.GameState.Value = "1"
            for x, player in pairs(plrs) do
                    --Code I would not like to show Its similar to the previous server script
                    else
                        if character:FindFirstChild("GameTag") then
                            --They are still alive
                            print(player.Name.." is still in the game!")
                        else
                            --They are dead
                            -- Code I would not like to show Its similar to the previous server script
                        end
                        print("teleported")
                        end
                    end
                else
                    table.remove(plrs,x)
                    print(player.Name.." has been removed!")
                end
            end

            Status.Value = "There are "..i.." seconds remaining"

            if i == 0 then
                RewardBestTDM(Reward)
                print(Status.Value)
                break
                end
            wait(1)
            end
    end 

    -- Cleanup Code
        Status.Value = "Game ended!"
        wait(5)
        game.Workspace.GameState.Value = "0"

Please help me!

0
Does it teleport when the timer is 1? You put the if status.Value == "1" then in the localscript, I am not sure which way are you trying to do it, they teleport if the timer is running, or they teleport when the timer ended. Torren_Mr 334 — 1y
0
It is supposed to teleport when the timer is running nkminion 21 — 1y
0
I just found an error at line 6, Can I make this server sided? nkminion 21 — 1y
0
Local scripts cannot access ServerStorage or ServerScriptService nkminion 21 — 1y

1 answer

Log in to vote
0
Answered by
JesseSong 3400 Moderation Voter Community Moderator
1 year ago
Edited 1 year ago

Local scripts cannot be accessed through a serverstorage or serverscriptservice. It can only work if you add a remote event through a serverscript to local scripts.n

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remfunction = ReplicatedStorage:WaitForChild( "Spawn")
script.Parent.MouseButton1Click:Connect(function()
    local Spawn = remfunction:InvokeServer()
end)

0
But,How to do that nkminion 21 — 1y
0
ill accept the answer once i figure it out, thanks! nkminion 21 — 1y
0
It didn't work, Ill update the question with the new scripts nkminion 21 — 1y
0
Updated nkminion 21 — 1y
View all comments (10 more)
0
Should I define the variables inside the function? nkminion 21 — 1y
0
Yes JesseSong 3400 — 1y
0
Still doesn't work i got an error nkminion 21 — 1y
0
I didnt know what you were talking about please be more specific. JesseSong 3400 — 1y
0
It doesnt teleport me when I press the button nkminion 21 — 1y
0
Im using a RemoteFunction nkminion 21 — 1y
0
What script can I help you on server or local JesseSong 3400 — 1y
0
Give me a couple minutes to edit the code JesseSong 3400 — 1y
0
I got another error nkminion 21 — 1y
0
I solved the question yay, Thanks a lot for the remotefunction idea nkminion 21 — 1y
Ad

Answer this question