Server script can't find model in workspace

I’m trying to make a turn-based combat system except my script can’t seem to find the “Fight” model in Workspace. In a localscript I clone the arena from replicatedstorage before the enemyturn event fire so it is loaded in, except it still can’t be found.

local rp = game:GetService("ReplicatedStorage")
local enemyturn = rp:WaitForChild("enemyturn")
local enemyattack = rp:WaitForChild("enemyattack")

enemyturn.OnServerEvent:Connect(function()
	print("the other thing fired")
	local enemy = game.Workspace.Fight.Enemy
    print("it knows its in workspace")
	local attackvalue = math.random(enemy.minattackvalue, enemy.maxattackvalue)
	wait(1.5)
	enemyattack:FireAllClients(attackvalue)
end)

How would I make the script find the “Fight” model?

Maybe try a WaitForChild?

local rp = game:GetService("ReplicatedStorage")
local enemyturn = rp:WaitForChild("enemyturn")
local enemyattack = rp:WaitForChild("enemyattack")

enemyturn.OnServerEvent:Connect(function()
	print("the other thing fired")
	local enemy = game.Workspace:WaitForChild("Fight").Enemy
    print("it knows its in workspace")
	local attackvalue = math.random(enemy.minattackvalue, enemy.maxattackvalue)
	wait(1.5)
	enemyattack:FireAllClients(attackvalue)
end)

If minattackvalue and maxattackvalue are IntValues or anything with a base value, I think you forgot to do .Value to get their value

Saw the question carefully, try Waiting as the post above me said

And if its locating it there where you’re defining the variable what line/script is throwing the error in output?

Putting a WaitForChild outputs an infinite yield for “Fight”, and it’s this script thats putting the error in the output.

Do FindFirstChild, then something that only runs if it returns true, also open the workspace and make sure that item is there
Server scripts don’t need WaitForChild, maybe in a few cases like if you don’t know when the item is added (if you used instance.new or clone something from another script)

What line does it say the error is caused on? the original variable reference?

The error is on line 7 where it tries to find the enemy and it says there’s an Infinite Yield on “Fight”. Or if I just try to reference it directly it says “Fight is not a valid member of Workspace “Workspace””

The item is in Workspace, it’s just not able to find it for some reason. Even if i do FindFirstChild

Is it in a folder or something? Did you check in workspace while you’re ingame and is the item added with Clone or Instance.new?

It’s cloned form ReplicatedStorage and then parented to Workspace based off the ServerEvent. In game it is in Workspace

I meant to say that once it’s parented it fires the serverevent

Try this and tell me what prints

local rp = game:GetService("ReplicatedStorage")
local enemyturn = rp:WaitForChild("enemyturn")
local enemyattack = rp:WaitForChild("enemyattack")

enemyturn.OnServerEvent:Connect(function()
	print("the other thing fired")
	local enemy = game.Workspace:FindFirstChild("Fight").Enemy
   
    if enemy then
          print("Found")
	      local attackvalue = math.random(enemy.minattackvalue, enemy.maxattackvalue)
	      wait(1.5)
	      enemyattack:FireAllClients(attackvalue)
     else
          print("Not found")
    end
end)

On line 7 it got an error, “attempted to index nil with ‘Enemy’”. Now I assume it found the model, but not the enemy inside of it. I’ll try to add another FindFirstChild and see if that fixes it

Alright, it clones on a server script right? Also when you start the game press this and it will change to the server
image
image
Then check in the workspace for the model

No, the model Fight returned nil, if “.Enemy” is removed it will print Not found
Do what I said in my last post

so if you place the model with a local script in the workspace then you can only see it on the client. it won’t exist for the server.

I think I understand now. I cloned it from a localscript. It’s not there on the server side, so I think if I clone it from the serverscript it should work. I forgot that the client-server applied to workspace as well.

I put all the loading into the server script and it works now. Thanks!

1 Like